2

I understand that this question is already posted and an answer has been given, but my case is different as I am populating multiple dropdown on a single method and so if I follow this link After every postback dropdownlist items repeats it will only retain the items in the last dropdownlist as it would clear all the lists before it.

Below would be my code, I use this method as a filter for 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;
    }

So is there a way for me to avoid getting the lists repeated on postback? Please let me know. Thanks in advance.

prkash
  • 427
  • 1
  • 5
  • 23
  • What do you mean by Repopulate? Are there duplicate entries? If not then are you using an `IsPostBack` check? – VDWWD Oct 18 '18 at 10:40
  • Yes duplicate entries. Every time it postback the list entries get duplicated for example the original list of 1 2 3 becomes 1 2 3 1 2 3 after postback. It happens on all the listboxes – prkash Oct 18 '18 at 10:41
  • Then you are not checking for IsPostBack somewhere probably. Each time the data gets added. – VDWWD Oct 18 '18 at 10:49

1 Answers1

2

Sounds like DataBind isn't clearing the old values, so you could try:

ddl.Items.Clear();

at the start of PopulateDropDown

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • Can you also tell me how I can make multiple selection with this? – prkash Oct 18 '18 at 12:08
  • I'm not too sure about asp.net, but normally you'd set 'item.selected = true' for each item, rather than set a single 'selecteditem' property. I'm not sure if asp.net supports it, so I found this article that might help: https://www.codeproject.com/Articles/18063/Multi-select-Dropdown-list-in-ASP-NET – Robin Bennett Oct 18 '18 at 12:53