c# - ASP.NET DropDownList in ListView's OnItemUpdating method always has a SelectedIndex of 0 -
in listview's edittemplate
, have dropdownlist control. dropdownlist populated in itemdatabound
method of listview
(only way found list populate in edittemplate....the dropdownlist exists in edittemplate; must label in itemtemplate
).
the list populates, , works. issue have when update button clicked , itemupdating
method called, selectedindex
of dropdownlist always zero, no matter value selected @ time.
the aspx code, minus templates looks this:
<asp:listview id="listview1" runat="server" onitemdatabound="listview1_itemdatabound" onitemupdating="listview1_itemupdating" onitemediting="listview1_itemediting"> <edititemtemplate> <tr style=""> <td><asp:dropdownlist cssclass="vaultdropdownlist" id="editstafflist" runat="server" autopostback="false" causesvalidation="true"></asp:dropdownlist></td> <td> <asp:linkbutton id="updatebutton" runat="server" commandname="update" text="update" /> <asp:linkbutton id="cancelbutton" runat="server" commandname="cancel" text="cancel" /> </td> </tr> </edititemtemplate> </asp:listview>
and codebehind:
protected void listview1_itemediting(object sender, listviewediteventargs e) { listview1.editindex = e.neweditindex; } protected void listview1_itemdatabound(object sender, listviewitemeventargs e) { if (e.item.displayindex == listview1.editindex) { dropdownlist ddl = e.item.findcontrol("editstafflist") dropdownlist; binddropdownlist(ddl); } } protected void listview1_itemupdating(object sender, listviewupdateeventargs e) { dropdownlist staff = listview1.items[e.itemindex].findcontrol("editstafflist") dropdownlist; //sql update stuff go here listview1.editindex = -1; }
if try populate dropdownlist in page_load
method use ispostback
, dropdown control returns null
.
any appreciated!
edit: listview bind:
protected void displaylistview(object sender, eventargs e) { string connstring = sqlstringtoolong; sqlconnection sqlconnection = new sqlconnection(connstring); datatable initialtable = new datatable(); string querystatement = "select lotsastuff"; sqldataadapter dataadapter = new sqldataadapter(querystatement, sqlconnection); dataadapter.fill(initialtable); listview1.datasource = initialtable; listview1.databind(); }
edit2: here's dropdownlist bind method:
public static void createstafflist(dropdownlist list, string searchgroup) { arraylist userlist = new arraylist(); //more sql stuff list.datasource = userlist; list.databind(); }
make sure aren't binding listview on every postback. wrap in condition check that.
protected void page_load(object sender, eventargs e) { if (!ispostback) { displaylistview(); } }
if don't, entire listview, including dropdownlists rebind. cause dropdownlist reset selectedindex 0.
Comments
Post a Comment