drop down menu - ASP.NET DropDownList not retaining selected item on postback -
i have asp dropdownlist gets populated on page_load event, after select item , hit button selected item gets cleared , first item in dropdownlist gets selected. (the dropdownlist populated when page not postback)
help please
if (!ispostback) { list<country> lcountries = new list<country>(); list<companyschedule> lcompanyschedules = new list<companyschedule>(); this.load_countries(lcountries); this.load_schedules(lcompanyschedules); if (personnelrec == null) { personnelrec = new personnel(); } if (request.querystring["ua"] != null && convert.toint32(request.querystring["ua"].tostring()) > 0) { useraccount.id = convert.toint32(request.querystring["ua"].tostring()); app_database.snapshift_select_helper.snapshift_select_personnel_app_account(ref useraccount); } this.imgemployeepicture.imageurl = "./images/employees/nophoto.gif"; if (request.querystring["ei"] != null && convert.toint32(request.querystring["ei"].tostring()) > 0) { this.load_personnelrec(convert.toint32(request.querystring["ei"].tostring())); } else { this.lblchangedirectionhead.enabled = false; this.lblchangedirections.enabled = false; this.lbschedules.disabled = true; } }
the page lifecycle following (plus other steps irrelevant question):
oninit
- populate controls viewstate (during postback)
- set selected values (during postback)
page_load
you need have viewstate enabled can populate list before "selects" item. in case, make sure don't repopulate in page_load , lose selected value. if (!ispostback) { // populate }
otherwise, have populate list manually in oninit
event on every page request. page_load
late in lifecycle, selected item lost.
edit:
the dropdownlist
must have valid values set (separate text displayed in browser). done through datavaluefield
property. each value must unique, otherwise first duplicate item ever selected. if @ html source in browser, should have:
<select> <option value="unique_value1">displayed text1</option> <option value="unique_value2">displayed text2</option> </select>
the unique values used selecting right item on server side.
Comments
Post a Comment