c# - Postback resets the selectedValue of DropdownList inside GridView -
my gridview has 2 columns - dropdownlist & textbox. ddl databound datatable. selectedindex change of ddl populate textbox , add new row. of works fine, selected values of ddls of previous rows, reset 0 index, when new row added.the textbox values remain intact though. how can retain ddl selected values when adding new rows.
for eg. if select 'abc' dropdown of row 1, populates text field , adds new row. postback happening, abc not remain selected in row 1 ddl:
login phone 1 123456789 2
aspx:
<asp:gridview id="gvcommissions" runat="server" onrowdatabound="gvcommissions_rowdatabound"> <columns> <asp:templatefield headertext="login" itemstyle-width="29%"> <itemtemplate> <asp:dropdownlist id="ddllogin" runat="server" width="98%" datatextfield="login" datavaluefield="id" onselectedindexchanged="ddllogin_selectedindexchanged" autopostback="true" > </asp:dropdownlist> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="phone" itemstyle-width="12%"> <itemtemplate> <asp:textbox id="txtphone" runat="server" width="98%" text='<%# eval("phone")%>' enabled="false"></asp:textbox> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
code behind:
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { //get values db, store datatable , viewstate getagentdetails(); //adds empty row grid setinitialrow(); } } private void addnewrow() { //create datatable dtcurrentdata = new datatable(); dtcurrentdata.columns.add("phone"); //store values of each row new datarow. add rows datatable foreach (gridviewrow gvrow in gvcommissions.rows) { datarow drcurrentrow = dtcurrentdata.newrow(); drcurrentrow["phone"] = ((textbox)gvrow.findcontrol("txtphone")).text; dtcurrentdata.rows.add(drcurrentrow); } //create empty datarow , add new datatable. datarow dr = dtcurrentdata.newrow(); dr["phone"] = ""; dtcurrentdata.rows.add(dr); //bind new datatable grid gvcommissions.datasource = dtcurrentdata; gvcommissions.databind(); } protected void gvcommissions_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { dropdownlist ddl = (dropdownlist)e.row.findcontrol("ddllogin"); dtagents = (datatable)viewstate["agentstable"]; ddl.datasource = dtagents; ddl.databind(); ddl.items.insert(0, ""); } }
when databind in addnewrow, original rows lost. must store dropdown selected index, textbox phone value in dtcurrentdata. can set index of dropdowns in rowdatabound event, using values saved in dtcurrentdata.
private void addnewrow() { //create datatable dtcurrentdata = new datatable(); dtcurrentdata.columns.add("phone"); //new column dropdown index dtcurrentdata.columns.add("ddlindex"); //store values of each row new datarow. add rows datatable foreach (gridviewrow gvrow in gvcommissions.rows) { datarow drcurrentrow = dtcurrentdata.newrow(); drcurrentrow["phone"] = ((textbox)gvrow.findcontrol("txtphone")).text; //get dropdown index drcurrentrow["ddlindex"] = ((dropdownlist)gvrow.findcontrol("ddllogin")).selectedindex; dtcurrentdata.rows.add(drcurrentrow); } //create empty datarow , add new datatable. datarow dr = dtcurrentdata.newrow(); dr["phone"] = ""; //initial drop down index dr["ddlindex"] = 0; dtcurrentdata.rows.add(dr); //save viewstate agentstable viewstate["currentdata"] = dtcurrentdata; //bind new datatable grid gvcommissions.datasource = dtcurrentdata; gvcommissions.databind(); } protected void gvcommissions_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { dropdownlist ddl = (dropdownlist)e.row.findcontrol("ddllogin"); dtagents = (datatable)viewstate["agentstable"]; ddl.datasource = dtagents; ddl.databind(); ddl.items.insert(0, ""); //get dropdown index currentdata //use current gridview's row index, since matches datatable if (viewstate["currentdata"] != null) { datatable dtcurrentdata = (datatable)viewstate["currentdata"]; ddl.selectedindex = convert.toint32(dtcurrentdata.rows[e.row.rowindex]["ddlindex"]); } } }
Comments
Post a Comment