c# - How to keep the checkbox inside gridview checked based on certain condition? -
i have gridview on web , checkbox inside template field. having field in database holds integer value 0 or 1. 0 enable , 1 disable. when check checkbox inserts 1 particular field in database , vice-versa. want when open page, rows value 1 in table should remain checked , rows value 0 in table should remain unchecked.i have tried doing this- aspx page-
<asp:gridview id="gridmain" runat="server" width="1000px" autogeneratecolumns="false" onrowdatabound="gridmain_rowdatabound"> <columns> <asp:templatefield headertext="student name"> <itemtemplate> <asp:label id="lblname" runat="server" text='<%# eval("name") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="enable/disable"> <itemtemplate> <asp:checkbox id="chkenbl" runat="server" autopostback="true" oncheckedchanged="chkenbl_checkedchanged" checked='<%# convert.toboolean(eval("en_dis")) %>' /> <br /> <asp:label id="label1" runat="server" text='<%# eval("id") %>'></asp:label> <asp:label id="label2" runat="server" text='<%# eval("en_dis") %>' visible="false"></asp:label> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
this cs page-
protected void page_load(object sender, eventargs e) { if (!ispostback) { show(); } //chkbind(); } public void show() { try { dt = g1.return_dt("select id,name,en_dis tbl_data_show order name"); if (dt.rows.count > 0) { adsource = new pageddatasource(); adsource.datasource = dt.defaultview; adsource.pagesize = 5; gridmain.datasource = adsource; gridmain.databind(); } } catch (exception ex) { response.write(ex.tostring()); } } protected void chkenbl_checkedchanged(object sender, eventargs e) { checkbox chk = (checkbox)sender; //checkbox chk = sender checkbox; //checkbox chk = (checkbox)row.findcontrol("chkenbl"); gridviewrow row = (gridviewrow)chk.namingcontainer; if (chk.checked) { try { //label lblid = (label)gridmain.findcontrol("label1"); //label lblid = new label(); //lblid.text = gridmain.findcontrol("label1").tostring(); string lblid = ((label)row.findcontrol("label1")).text; rows = g1.execdb("update tbl_data_show set en_dis='1' id=" + lblid); if (rows > 0) { response.write("rows effected successfull."); } } catch (exception ex) { response.write(ex.tostring()); } } else { //label lblid1 = (label)gridmain.findcontrol("label1"); //label lblid1 = new label(); //lblid1.text = gridmain.findcontrol("label1").tostring(); string lblid1 = ((label)row.findcontrol("label1")).text; rows = g1.execdb("update tbl_data_show set en_dis='0' id=" + lblid1); if (rows > 0) { response.write("rows effected successfull."); } } } protected void gridmain_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { dt = g1.return_dt("select en_dis tbl_data_show"); if (dt.rows.count > 0) { // checkbox chk1 = new checkbox(); checkbox chk1 = (checkbox)e.row.findcontrol("chkenbl"); //checkbox if (dt.rows[0]["en_dis"] == "0") { chk1.checked = false; } else { chk1.checked = true; } } } }
please guide me doing wrong?
in markup can add checked='<%# convert.toboolean(eval("en_dis")) %>'
. see example below:
<asp:checkbox id="chkenbl" runat="server" autopostback="true" checked='<%# convert.toboolean(eval("en_dis")) %>' oncheckedchanged="chkenbl_checkedchanged" />
or
- use
onrowdatabound
event of gridview. - make asp hidden field keep value of
en_dis
column of datatable using same eval function. - find checkbox control , make checked or unchecked based on value retrieved hidden field control.
Comments
Post a Comment