asp.net - Data binding fails to change values for custom control inside GridView inside an UpdatePanel -
i creating gridview inside updatepanel this:
<asp:scriptmanager runat="server" enablepartialrendering="true" /> <div id="masterhistorydialog" style="display: none"> <asp:updatepanel runat="server" childrenastriggers="true" updatemode="always"> <triggers> <asp:asyncpostbacktrigger controlid="historyrepeater" eventname="pageindexchanging" /> </triggers> <contenttemplate> <asp:gridview id="historyrepeater" autogeneratecolumns="false" runat="server" allowpaging="true" allowsorting="false" pagersettings-nextpagetext="next" pagersettings-previouspagetext="previous" pagesize="4" onpageindexchanging="historyrepeater_pageindexchanging"> <columns> <asp:templatefield> <itemtemplate> <uc:customcalendarcontrol changeid='<%# databinder.eval(container.dataitem, "item1") %>' scheduleid='<%# databinder.eval(container.dataitem, "item2") %>' runat="server" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </contenttemplate> </asp:updatepanel> </div> the pageindexchanging event fires expected, calling following event handler:
protected sub historyrepeater_pageindexchanging(sender object, e gridviewpageeventargs) historyrepeater.pageindex = e.newpageindex historyrepeater.datasource = getschedules() historyrepeater.databind() end sub customcalendarcontrol custom control.
i confirmed getschedules is, in fact, getting entire data set.
i page data , have 4 custom controls per page (total of 20 items on 5 pages). first page works correctly. however, when try navigate 1 of other pages, initialization of custom control fails because it's not getting actual values of changeid or scheduleid - they're 0.
it fails when call databind() because initialization of custom control depends on changeid , scheduleid being actual ids.
how can fix issue?
your problem has @ moment nested user controls populate client side content. happens first control , gridview, usercontrol has been build before values of changeid , scheduleid set.
one way solved manually call building of control parent.
first, modify usercontrol has public function display values
public string changeid { get; set; } public string scheduleid { get; set; } protected void page_load(object sender, eventargs e) { setucvalues(); } public void setucvalues() { label1.text = changeid; label2.text = scheduleid; } public property changeid string end set end set end property public property scheduleid string end set end set end property protected sub page_load(byval sender object, byval e eventargs) setucvalues end sub public sub setucvalues() label1.text = changeid label2.text = scheduleid end sub next, add id usercontrol in gridview
<uc:customcalendarcontrol id="calendarcontrol" runat="server" changeid= then in pageindexchanging, loop rows , call function
protected void historyrepeater_pageindexchanging(object sender, gridviewpageeventargs e) { //databinding foreach (gridviewrow row in historyrepeater.rows) { webusercontrol1 control = row.findcontrol("calendarcontrol") webusercontrol1; control.setucvalues(); } } protected sub historyrepeater_pageindexchanging(sender object, e gridviewpageeventargs) 'databinding each row gridviewrow in historyrepeater.rows dim control webusercontrol1 = ctype(row.findcontrol("calendarcontrol"),webusercontrol1) control.setucvalues next end sub
Comments
Post a Comment