asp.net - Dynamically dropping grid columns - effect on postbacks -
i have read number of threads here on subject of posting data dynamically added datagrid columns. i've read says since columns added after init phase, not available in control collection upon postback , therefore data posted these dynamically added columns not available , cannot restored viewstate unless re-created in page_init. have read long controls re-added identical ids ( i.e ids should match match posted control ids), restored viewstate.
what if i'm not dynamically adding grid columns rather dropping grid columns in code behind. id's of columns remmin should match in control collection begin data posted should resotored right not i'm seeing. suppos following list of columns in aspx page. if in code behind drop "test" column check checkbox in first column. upon postback, checkbox selection not maintained. have expected based on i've read. can correct understanding on this?
<columns> <asp:templatecolumn headertext="" headerstyle-width="1%" itemstyle-horizontalalign="center" footerstyle-cssclass="datagridfooter" footerstyle-horizontalalign="center" footerstyle-verticalalign="top"> <headertemplate> <input id="checkall" type="checkbox" onclick="checkalldatagridcheckboxes('checkboxisselected',this.checked)"/> </headertemplate> <itemtemplate> <asp:checkbox runat="server" id="checkboxisselected" checked='false' /> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="subject" headerstyle-width="10%" visible="true" itemstyle-horizontalalign="center" footerstyle-cssclass="datagridfooter" footerstyle-horizontalalign="center" footerstyle-verticalalign="top" > <itemtemplate> <asp:label id="labelsubjectid" runat="server" text='<%# databinder.eval(container.dataitem, "subjectid") %>' /> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="test" headerstyle-width="5%" visible="true" itemstyle-horizontalalign="center" footerstyle-cssclass="datagridfooter" footerstyle-horizontalalign="center" footerstyle-verticalalign="top" > <itemtemplate> <asp:label id="labeltestcode" runat="server" text='<%# databinder.eval(container.dataitem, "testcode") %>' /> </itemtemplate> </asp:templatecolumn> </columns>
added bindgrid() , event handler buttonapprove_click. method setgridcolumns 1 drops grid columns. have included too.
private void bindgrid() { try { setmasterdetailcontrolstates(ispageindetailsmode()); if (!validateargs()) { gridbias.visible = false; return; } foreach (listitem displayoption in chkboxdisplay.items) { if (displayoption.selected) { if (displayoption.value == "highlight") { _showhighlighting = true; } } } /* <snipped>...make procedure call dataset */ dataset ds = sp.getdataset(); if (sp.rc != (int)returncode.success) { if ((sp.rc == (int)returncode.no_records_returned)) { labelmessage.text = "no data found specified filter"; } else if ((sp.rc == (int)returncode.unhandled_exception) || (ds == null)) { labelmessage.text = sp.errormessage; } ds = null; gridbias.datasource = ds; enablecontrols(false); setgridcolumns(); } else { gridbias.titletext = buildgridtitlestring(); gridbias.filtertext = buildgridfilterstring(); gridbias.enablerowsperpageedit = true; setgridcolumns(); //since grid columns dynamically added , can change between postbacks //make sure sort column valid, if not unset if (gridbias.sortcolumn != null && gridbias.sortcolumn.length > 0) { bool foundcol = false; foreach (datagridcolumn dg in gridbias.columns) { if (gridbias.sortcolumn == dg.sortexpression) { foundcol = true; break; } } if (!foundcol) { gridbias.sortcolumn = ""; gridbias.sortorder = ""; } } if (gridbias.sortcolumn.length == 0) gridbias.sortcolumn = "testcode"; if (gridbias.sortorder.length == 0) gridbias.sortorder = "asc"; if (gridbias.pageindex.length != 0 && !gridbias.gridviewmode.equals(customdatagrid.gridviewtype.showall.tostring())) gridbias.currentpageindex = convert.toint32(gridbias.pageindex); ds.tables[0].defaultview.sort = gridbias.sortcolumn + " " + gridbias.sortorder; gridbias.datasource = ds.tables[0].defaultview; enablecontrols(true); } gridbias.hideemptycolumns = true; arraylist cols = new arraylist(); cols.addrange(_alrecheckcolumns); gridbias.hideemptycolumnslist = cols; gridbias.databind(); } catch (exception ex) { codelibrary.errorhandling.setlabel("error(" + codelibrary.errorhandling.getmethodname() + ")", ex.message, labelmessage, true, "errormsg"); } } private void setgridcolumns() { arraylist alcols = new arraylist(); alcols.add(""); //checkbox column alcols.add("subject"); arraylist alcolumnstobedeleted = new arraylist(); boolean boolcolexists = false; foreach (listitem chart in chkboxcharttype.items) { if (chart.value == "%bias") { if (rblbiasoptions.selectedvalue == "subject") { foreach (datagridcolumn dg in gridbias.columns) { boolcolexists = false; foreach (string gridcolumn in alcols) { if (dg.headertext == gridcolumn) { boolcolexists = true; break; } } if (!boolcolexists) { alcolumnstobedeleted.add(gridbias.columns.indexof(dg)); } } } } // loop thru array list , delete columns grid. int count = 0; foreach (int in alcolumnstobedeleted) { gridbias.columns.removeat(i - count); //subtract index columns deleted. count++; } } protected void buttonapprove_click(object sender, eventargs e) { try { checkpermission(this.moduleid, this.gettype().basetype.tostring(), "buttonapprove_click"); foreach (datagriditem in gridbias.items) { //loop thru each row , determien values saved db if (i.itemtype == listitemtype.item || i.itemtype == listitemtype.alternatingitem) { bool cbselected = ((checkbox)i.findcontrol("checkboxisselected")).checked; if (cbselected) { bool isapproved = true; string strsubjectid = ((system.web.ui.webcontrols.label)i.findcontrol("labelsubjectid")).text; string strtestcode = ((system.web.ui.webcontrols.label)i.findcontrol("labeltestcode")).text; string strsampletype = ((system.web.ui.webcontrols.label)i.findcontrol("labelsampletype")).text; string strtestcodeandtype = ((system.web.ui.webcontrols.label)i.findcontrol("labeltestcodeandtype")).text; if (((system.web.ui.webcontrols.label)i.findcontrol("labelrecheckneedsapproval")).text == "1") isapproved = true; else isapproved = false; savetodb(strsubjectid, strtestcode, strsampletype, strtestcodeandtype,isapproved); } } } bindgrid(); } catch (exception exceptionobject) { string strexceptionmessage = exceptionobject.message + "-" + exceptionobject.innerexception.message; codelibrary.errorhandling.setlabel("", strexceptionmessage, labelmessage, false, "errormsg"); //--- set label text } }
Comments
Post a Comment