c# - Retain dropdown values in gridview -
i have having grid use checkbox edit grid rows. on checkbox click how can retain dropdown values?
<columns> <asp:templatefield> <headertemplate> <asp:checkbox id="chkall" runat="server" autopostback="true" oncheckedchanged="oncheckedchanged" /> </headertemplate> <itemtemplate> <asp:checkbox id="checkbox1" runat="server" autopostback="true" oncheckedchanged="oncheckedchanged" /> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="scope"> <headerstyle horizontalalign="center" wrap="false" cssclass="header"> </headerstyle> <itemtemplate> <asp:label id="lblscope" runat="server" text='<%# bind("scope") %>'></asp:label> <asp:dropdownlist id="ddlcms" visible="false" runat="server"> <asp:listitem>yes</asp:listitem> <asp:listitem>no</asp:listitem> </asp:dropdownlist> </itemtemplate> <itemstyle wrap="false" cssclass="header" /> </asp:templatefield> </columns>
i using below code check , uncheck grid rows editing. when click on checkbox not able retail gridview selected dropdown values. ex: third row has column called scope has selected value no. when click on checkbox values yes since order have binded in dropdown.
protected void oncheckedchanged(object sender, eventargs e) { bool isupdatevisible = false; checkbox chk = (sender checkbox); if (chk.id == "chkall") { foreach (gridviewrow row in updates.rows) { if (row.rowtype == datacontrolrowtype.datarow) { row.cells[0].controls.oftype<checkbox>().firstordefault().checked = chk.checked; } } } checkbox chkall = (updates.headerrow.findcontrol("chkall") checkbox); chkall.checked = true; foreach (gridviewrow row in updates.rows) { if (row.rowtype == datacontrolrowtype.datarow) { bool ischecked = row.cells[0].controls.oftype<checkbox>().firstordefault().checked; (int = 1; < row.cells.count; i++) { if (row.cells[i].controls.oftype<label>().tolist().count > 0) { row.cells[i].controls.oftype<label>().firstordefault().visible = !ischecked; } if (row.cells[i].controls.oftype<textbox>().tolist().count > 0) { row.cells[i].controls.oftype<textbox>().firstordefault().visible = ischecked; } if (row.cells[i].controls.oftype<dropdownlist>().tolist().count > 0) { row.cells[i].controls.oftype<dropdownlist>().firstordefault().visible = ischecked; } if (ischecked && !isupdatevisible) { isupdatevisible = true; } if (!ischecked) { chkall.checked = false; } } } } btnsave.visible = isupdatevisible; }
first create function this
private dataset getdata(string query) { string constring = configurationmanager.connectionstrings["connectionstring"].connectionstring; sqlcommand cmd = new sqlcommand(query); using (sqlconnection con = new sqlconnection(constring)) { using (sqldataadapter da = new sqldataadapter()) { cmd.connection = con; da.selectcommand = cmd; using (dataset ds = new dataset()) { da.fill(ds); return ds; } } } }
and on gridview rowdatabound
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { //find dropdownlist in row dropdownlist ddlrole = (e.row.findcontrol("ddlrole") dropdownlist); ddlrole.datasource = getdata("select roletype tablename"); ddlrole.datatextfield = "roletype"; ddlrole.datavaluefield = "roletype"; ddlrole.databind(); //add default item in dropdownlist ddlrole.items.insert(0, new listitem("please select")); //select role of user in dropdownlist string role = (e.row.findcontrol("lblrole") label).text; ddlrole.items.findbyvalue(role).selected = true; } }
ok may can try viewstate m not sure may these link you
Comments
Post a Comment