c# - CheckBox checked become unchecked after sorting or paging (not GridView) -
i have checkbox in aspx form page, external gridview.
if checked checkbox executed 1 query populated gridview , update rows.
this gridview paginated , when change page returns initial gridview rows , lost selection in checkbox.
is there way keep checked after sorting , paging?
thanks.
edit #1
my code below.
when checkbox checked in binddata(); executed different query initial.
int ck = 0; protected void page_load(object sender, eventargs e) { if (!page.ispostback) { binddata(); } } protected void ddlpages_selectedindexchanged(object sender, eventargs e) { gridviewrow gvrpager = gvproducts.bottompagerrow; dropdownlist ddlpages = (dropdownlist)gvrpager.cells[0].findcontrol("ddlpages"); gvproducts.pageindex = ddlpages.selectedindex; binddata(); } protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e) { gvproducts.pageindex = e.newpageindex; binddata(); } protected void paginate(object sender, commandeventargs e) { int intcurindex = gvproducts.pageindex; switch (e.commandargument.tostring().tolower()) { case "first": gvproducts.pageindex = 0; break; case "prev": gvproducts.pageindex = intcurindex - 1; break; case "next": gvproducts.pageindex = intcurindex + 1; break; case "last": gvproducts.pageindex = gvproducts.pagecount - 1; break; } gvproducts.databind(); } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { if (convert.toint32(databinder.eval(e.row.dataitem, "money")) > 100000) { e.row.backcolor = system.drawing.color.antiquewhite; } } if (e.row.rowtype == datacontrolrowtype.pager) { dropdownlist ddl = (dropdownlist)(e.row.findcontrol("ddlpages")); label lblpagecount = (label)e.row.findcontrol("lblpagecount"); if (lblpagecount != null) lblpagecount.text = gvproducts.pagecount.tostring(); (int = 1; <= gvproducts.pagecount; i++) { ddl.items.add(i.tostring()); } ddl.selectedindex = gvproducts.pageindex; if (gvproducts.pageindex == 0) { ((imagebutton)e.row.findcontrol("imagebutton1")).visible = false; ((imagebutton)e.row.findcontrol("imagebutton2")).visible = false; } if (gvproducts.pageindex + 1 == gvproducts.pagecount) { ((imagebutton)e.row.findcontrol("imagebutton3")).visible = false; ((imagebutton)e.row.findcontrol("imagebutton4")).visible = false; } } } protected void chklinked_checkedchanged(object sender, eventargs args) { if (sender != null) { try { if (((checkbox)sender).checked) { ck = 1; binddata(); } else { ck = 0; binddata(); } } catch { } } }
edit #2
private dataset retrieveproducts() { if (viewstate["products"] != null) return (dataset)viewstate["products"]; if (ck > 0) { sql = @" select * dotable money > 100000; "; } else { sql = @" select * dotable; "; } dataset dsproducts = new dataset(); using (odbcconnection cn = new odbcconnection(configurationmanager.connectionstrings["connmysql"].connectionstring)) { cn.open(); using (odbccommand cmd = new odbccommand(sql, cn)) odbcdataadapter adapter = new odbcdataadapter(cmd); adapter.fill(dsproducts); } } return dsproducts; } private void binddata() { gvproducts.datasource = retrieveproducts(); gvproducts.databind(); }
the possible reason can viewstate not enabled check box why not preserving state.to enable can use enableviewstate property of checkbox.
<asp:checkbox id="chklinked" runat="server" enableviewstate="true"/>.
Comments
Post a Comment