从 Objectdatasource 上的 FetchCount() 访问时,Hiddenfield 为 null
我正在使用 subsonic 和带有 objectdatasource 的 gridview (我之前没有使用过 objectdatasource)
网格绑定得很好,我唯一的问题是我不想继续调用数据库来获取表中所有记录的计数。因此,当我第一次调用“FetchCount”(SelectCountMethod)时,我想将其存储在隐藏字段(或视图状态)中。由于某种原因,当我尝试访问它时,我的隐藏字段始终为空,而不是值,即实际的隐藏字段。如果我尝试将其存储在视图状态中,情况也是如此。
这是我的 aspx.只是一个 gridview、ObjectDatasource 和一个隐藏字段
<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" 是上述控件所在网页的命名空间。
public int? TotalRecords
{
get
{
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 that uses subsonic to return a collection of Properties from the database //and passes in the page index and count
}
。
我做错了什么?
我的解决方案
我使用了会话
I am using subsonic and a gridview with objectdatasource (I have not used an objectdatasource before)
The grid binds just fine, my only problem is that I don't want to keep calling the database to get a count of all the records in the table. So when I call "FetchCount" (the SelectCountMethod) the first time I want to store it ina hiddenfield (or viewstate). For some reason my hiddenfield is always null when I try and access it, not the value, the actual hidden field. This is also the case if I try storing it in the viewstate.
This is my aspx. Just a gridview, ObjectDatasource and a 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" is the namespace of the webpage that the above controls are on.
public int? TotalRecords
{
get
{
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 that uses subsonic to return a collection of Properties from the database //and passes in the page index and count
}
.
What am I doing wrong?
MY SOLUTION
I used the session instead
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您尝试将
SearchResults
用作Page
和支持ObjectDataSource
的基础类。正如文档中所述,由于您的数据对象方法不是
静态
,ObjectDataSource
将创建SearchResults
类的新实例,并且调用该实例上的方法。这意味着隐藏字段将始终为null
并且视图状态内容将毫无意义。您可以将记录计数保留在 ASP.NET 会话状态中,您可以从当前 HTTP 上下文访问该状态:
That's because you're trying to use
SearchResults
as both aPage
and the underlying class backing up theObjectDataSource
.As explained in the documentation, since your data object methods are not
static
, theObjectDataSource
will create a new instance of theSearchResults
class and call the methods on that instance. This means the hidden field will always benull
and the view state contents will be meaningless.You can persist the record count in the ASP.NET session state instead, which you can access from the current HTTP context: