c# - Hiddenfield is null when accessing from FetchCount() on Objectdatasource -
i using subsonic , gridview objectdatasource (i have not used objectdatasource before)
the grid binds fine, problem don't want keep calling database count of records in table. when call "fetchcount" (the selectcountmethod) first time want store ina hiddenfield (or viewstate). reason hiddenfield null when try , access it, not value, actual hidden field. case if try storing in viewstate.
this aspx. gridview, objectdatasource , hiddenfield
<asp:gridview id="grdresults" runat="server" allowpaging="true" autogeneratecolumns="false" datasourceid="propertiesdatasource" pagesize="10" > <columns > <asp:boundfield datafield="propertyname" /> </columns> </asp:gridview> <asp:objectdatasource id="propertiesdatasource" runat="server" selectmethod="fetchpageddata" typename="testwebsite.usercontrols.search.searchresults" selectcountmethod="fetchcount" startrowindexparametername="start" maximumrowsparametername="pagelength" enablepaging="true" /> <asp:hiddenfield id="hdntotalrecords" runat="server" />
typename="testwebsite.usercontrols.search.searchresults" namespace of webpage above controls on.
public int? totalrecords { { if (hdntotalrecords.value != string.empty) return int.parse(hdntotalrecords.value); else return null; } set { hdntotalrecords.value = value.tostring(); } } protected void page_load(object sender, eventargs e) { if (!ispostback) grdresults.databind(); } [dataobjectmethod(dataobjectmethodtype.select, false)] public propertyxcollection fetchpageddata(int start, int pagelength) { int startindex; if (start == 0) startindex = 1; else startindex = start / pagelength + 1; propertyxcollection collection = getproperties(startindex, 10); return collection; } public int fetchcount() { int returnval = 0; if (totalrecords != null) returnval = (int)totalrecords; else { totalrecords = getproperties(null, null).count; returnval = (int)totalrecords; } return (int)returnval; } propertyxcollection getproperties(int? pageindex, int? pagecount) { //method uses subsonic return collection of properties database //and passes in page index , count }
.
what doing wrong?
my solution
i used session instead
that's because you're trying use searchresults
both page
, underlying class backing objectdatasource
.
as explained in documentation, since data object methods not static
, objectdatasource
create new instance of searchresults
class , call methods on instance. means hidden field null
, view state contents meaningless.
you can persist record count in asp.net session state instead, can access current http context:
public int? totalrecords { { return (int?) httpcontext.current.session["totalrecords"]; } set { httpcontext.current.session["totalrecords"] = value; } }
Comments
Post a Comment