c# - GridView still being refreshed when not in UpdatePanel -
problem , description:
i have gridview programmatically added dropdownlists on rowdatabound
every cell.
the dropdownlists have data sources
.
when press 'save' button, meant read selecteditems
in dropdownlists , save them database.
however, when click on button, causes postback
, deletes of controls in gridview , therefore, none of them can found in button_click
event.
after asking questions before , trying different techniques try store dropdownlists in cache/session state etc, can't seem able keep dropdownlists or data when click button.
therefore, trying add updatepanel , use ajax try stop refresh of gridview.
my attempt such:
<asp:gridview id="gv_rota" runat="server" autogeneratecolumns="false" onrowdatabound="gv_rota_rowdatabound"> <headerstyle backcolor="#6a3d98" forecolor="white" height="20" /> <rowstyle horizontalalign="center" height="20px" width="100px" /> <alternatingrowstyle height="20px" /> </asp:gridview> </div> <asp:label id="lbl_nameofrota" runat="server" text="new rota name:"></asp:label> <input runat="server" id="txt_rotaname" /> <asp:updatepanel runat="server" id="rotaupdatepanel"> <contenttemplate> <asp:button id="btn_addrota" runat="server" text="add" onclick="btn_addrota_click" cssclass="buttonadminpage" /> </contenttemplate> </asp:updatepanel>
as can see, placed updatepanel around button only, however, still seemed refresh gridview , delete controls.
questions:
firstly, why did attempt not work?
secondly, how can around problem using ajax , update panels, or there method me need? (baring in mind, if else suggested, i've tried it).
edit 2:
supplied code:
this when bind dropdownlists:
protected void gv_rota_rowdatabound(object sender, gridviewroweventargs e) { (int = 1; <= columncount; i++) { if (e.row.rowtype == datacontrolrowtype.datarow) { int day = e.row.rowindex; day += 1; ddlshift = new dropdownlist(); ddlshift.id = "ddlshift" + "wk" + i.tostring() + "day" + day.tostring(); ddlshift.datasource = dclistofshifts; ddlshift.datavaluefield = "shift_id"; ddlshift.datatextfield = "shift_name"; ddlshift.attributes.add("place", i.tostring()); ddlshift.databind(); ddlshift.items.insert(0, new listitem("shift...")); ddlshift.cssclass = "ddl_rotamanager"; e.row.cells[i].controls.add(ddlshift); } } }
this when create gridview dependant on amount of columns passed:
private void bindgrid(int amount) { gv_rota.datasource = null; gv_rota.columns.clear(); boundfield bfield = new boundfield(); bfield.headertext = "days"; bfield.datafield = "days"; gv_rota.columns.add(bfield); (int = 0; < amount; i++) { int week = + 1; string sweek = "week " + week.tostring(); templatefield tfield = new templatefield(); tfield.headertext = sweek; gv_rota.columns.add(tfield); } datatable dt = new datatable(); dt.columns.add(new datacolumn("days", typeof(string))); dt.rows.add("m"); dt.rows.add("t"); dt.rows.add("w"); dt.rows.add("t"); dt.rows.add("f"); dt.rows.add("s"); dt.rows.add("s"); gv_rota.datasource = dt; gv_rota.databind(); }
this selected amount of columns , call method create gridview, store amount in cache:
protected void ddl_rotaamountofweeks_selectedindexchanged(object sender, eventargs e) { if (ispostback) { int amount; int.tryparse(ddl_rotaamountofweeks.selecteditem.tostring(), out amount); columncount = amount; cache.add("columncount", columncount, null, cache.noabsoluteexpiration, new timespan(0, 60, 0), cacheitempriority.default, null); bindgrid(amount); } }
button code:
protected void btn_addrota_click(object sender, eventargs e) { //my first attempt @ trying save gridview, realising cache'd gridview before selecting items. //gv_rota = (gridview)cache["cachegridview"]; //set of size of array amount of rows * colums //this maximum amount of events added int arraysize = gv_rota.rows.count * (int)cache["columncount"]; //the current array item being added int arrayitem = 0; //finally array of rota_events pass wcf wspersonnel.dc_wfm_rota_events[] arrayofrotaevents = new wspersonnel.dc_wfm_rota_events[arraysize]; foreach (gridviewrow row in gv_rota.rows) { (int = 1; <= (int)cache["columncount"]; i++) { int day = row.rowindex; day += 1; dropdownlist ddl1 = (dropdownlist)gv_rota.rows[row.rowindex].cells[i].findcontrol("ddlshiftwk" + i.tostring() + "day" + day.tostring()); wspersonnel.dc_wfm_rota_events dcevent = new wspersonnel.dc_wfm_rota_events { shift_id = ddl1.selecteditem.value, week = i, weekspecified = true, day = day, dayspecified = true, }; arrayofrotaevents[arrayitem++] = dcevent; } } wsp.addrota(new wspersonnel.dc_wfm_rota { rota_name = txt_rotaname.value, period_type = 1, period_typespecified = true, period_amount = columncount, period_amountspecified = true, rota_events = arrayofrotaevents }); }
example of gridview looks like:
if want stop postback on button click inside update panel can try this
<asp:updatepanel runat="server" id="rotaupdatepanel" updatemode="conditional"> <contenttemplate> <asp:button id="btn_addrota" runat="server" text="add" onclick="btn_addrota_click" cssclass="buttonadminpage" clientidmode="autoid"/> </contenttemplate> <triggers> <asp:asyncpostbacktrigger controlid="btn_addrota" eventname="click" /> </triggers> </asp:updatepanel>
Comments
Post a Comment