c# - DropDownList lose index after PostBack -


i have dropdown fill data sql server.

i fill dropdown dynamically in page_init() event. depending on value, listitem selected.

now problem is, when select item in dropdown, after postback selection reset first item in dropdownlist.

this here basic version of code not work:

        arraylist ad_group_members = activedirectory.getmemberofgroup("ad-group");         arraylist listmachines = sqlquery.read("database", "select idvm, randomstring, computername, owner, vm order computername");          (int = 0; < 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);             (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 issue in code, doesn't work example. thank in advance

you have populate dropdownlist under condition on pageload. because on every post ddl populated again , loses selected index.

if (!ispostback) {     //populateyourddl here } 

Comments