DropDownList lose index after PostBack

2019-09-09 22:53发布

问题:

I have a dropdown which I fill with data from a SQL server.

I fill the dropdown dynamically in the Page_Init() Event. Depending on the Value, a ListItem is selected.

Now the problem is, that when I select another Item in the dropdown, that after the postback the selection is reset to the first item in the dropdownlist.

This here is a basic version of my code which does not work:

        ArrayList AD_Group_Members = ActiveDirectory.GetMemberOfGroup("AD-Group");
        ArrayList ListMachines = SQLQuery.Read("Database", "SELECT idVM, RandomString, Computername, Owner, FROM VM ORDER BY Computername");

        for (int i = 0; i < ListMachines.Count; i++)
        {
            String RandomString = ((Hashtable)ListMachines[i])["RandomString"].ToString();
            String Owner = ((Hashtable)ListMachines[i])["Owner"].ToString();
            DropDownList DropDownList_Owner = new DropDownList();
            DropDownList_Owner.ID = "DropDownList_Owner_" + RandomString;
            DropDownList_Owner.Width = Unit.Percentage(95);
            DropDownList_Owner.AutoPostBack = true;
            DropDownList_Owner.EnableViewState = true;
            DropDownList_Owner.SelectedIndexChanged += DropDownList_Owner_SelectedIndexChanged;
            Div_Test.Controls.Add(DropDownList_Owner);
            for (int y = 0; y < AD_Group_Members.Count; y++)
            {
                ListItem ListItem = new ListItem();
                ListItem.Value = Owner;
                ListItem.Text = ((Hashtable)AD_Group_Members[y])["GivenName"].ToString() + " " + ((Hashtable)AD_Group_Members[y])["Surname"].ToString();
                if (((Hashtable)AD_Group_Members[y])["Username"].ToString().Equals(Owner))
                {
                    ListItem.Selected = true;
                }
                DropDownList_Owner.Items.Add(ListItem);
            }
        }

Where is the issue in my code, that it doesn't work but the example. Thank in Advance

回答1:

You have to populate your dropdownlist under this condition on pageload. Because on every post back your ddl is populated again and loses its selected index.

if (!IsPostBack)
{
    //PopulateYourDDL here
}


回答2:

You filled dropdown in Page_Init() which gets called in every postback and refill your dropdown and hence loses selectedindex.So you have to fill your dropdown inside !ispostback block

if (!IsPostBack)
{
   //fill your dropdown here
}


回答3:

I think the you should have unique values for the dropdown. Also as you have duplicate values in data value field the problem is occurring. It is looking for the first match and selecting it. You could try to fabricate the values which you could uniquely identify. Something like below:

COLUMN_NAME  DATA_TYPE
a            a_decimal
b            b_decimal
c            c_decimal
d            d_int
e            e_int
f            f_varchar
g            g_varchar
h            h_varchar
i            i_varchar
j            j_varchar

Check out this Useful Source!!! I hope it helps!!! Have a close look at those comments in the accepted answer section !!!

Also avoid using SelectedIndex_Changed() functions while dealing with Dynamically generated Web controls. Bind the DropdownList under Page_Init() or Page_PreInit(). If you want to perform some functions upon DropDownlist Selection Check out this! to determine the WebControl ID which is triggered and then perform an unique function in the Page_PreInit() or Page_Init().