gpt4 book ai didi

c# - 回发时列表框重复(重复)

转载 作者:行者123 更新时间:2023-11-30 22:58:40 31 4
gpt4 key购买 nike

我知道这个问题已经发布并且已经给出了答案,但我的情况不同,因为我在单个方法上填充多个下拉列表,所以如果我点击此链接 After every postback dropdownlist items repeats它只会保留最后一个下拉列表中的项目,因为它会清除它之前的所有列表。

下面是我的代码,我使用这个方法作为 gridview 的过滤器。

private void BindDropDownList()
{
PopulateDropDown(ddlgvsite, lblsite.Text);
PopulateDropDown(ddlgvskill, lblskill.Text);
PopulateDropDown(ddlgvshift, lblshift.Text);
PopulateDropDown(ddlgvtm, lbltm.Text);
PopulateDropDown(ddlgvgrpm, lblgrpm.Text);
PopulateDropDown(ddlgvopsm, lblopsm.Text);
PopulateDropDown(ddlgvltype, lblltype.Text);
}
private void PopulateDropDown(ListBox ddl, string columnName)
{
ddl.DataSource = BindDropDown(columnName);
ddl.DataTextField = columnName;
ddl.DataValueField = columnName;
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("Please Select", "0"));
}
private void setDropdownselectedItem(string selectedvalue, ListBox ddl)
{
if (!string.IsNullOrEmpty(selectedvalue))
{
ddl.Items.FindByValue(selectedvalue).Selected = true;
}
}
protected void DropDownChange(object sender, EventArgs e)
{
ListBox dropdown = (ListBox)sender;
string selectedValue = dropdown.SelectedItem.Value;
switch (dropdown.ID.ToLower())
{
case "ddlgvsite":
ViewState["Site"] = selectedValue;
break;
case "ddlgvskill":
ViewState["Skill"] = selectedValue;
break;
case "ddlgvshift":
ViewState["Shift"] = selectedValue;
break;
case "ddlgvtm":
ViewState["TM"] = selectedValue;
break;
case "ddlgvgrpm":
ViewState["GrpM"] = selectedValue;
break;
case "ddlgvopsm":
ViewState["OpsM"] = selectedValue;
break;
case "ddlgvltype":
ViewState["Ltype"] = selectedValue;
break;
}
this.BindGrid();
}
private DataTable BindDropDown(string columnName)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
MySqlConnection con = new MySqlConnection(strConnString);
MySqlCommand cmd = new MySqlCommand("SELECT DISTINCT (" + columnName + ") FROM approved WHERE " + columnName + " IS NOT NULL", con);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}

那么有没有办法避免在回发时重复列表?请告诉我。提前致谢。

最佳答案

听起来 DataBind 没有清除旧值,所以您可以尝试:

ddl.Items.Clear();

在 PopulateDropDown 的开始

关于c# - 回发时列表框重复(重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871926/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com