从 Objectdatasource 上的 FetchCount() 访问时,Hiddenfield 为 null

发布于 2024-12-09 14:26:31 字数 2445 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2024-12-16 14:26:32

这是因为您尝试将 SearchResults 用作 Page 和支持 ObjectDataSource 的基础类。

正如文档中所述,由于您的数据对象方法不是静态ObjectDataSource 将创建 SearchResults 类的实例,并且调用该实例上的方法。这意味着隐藏字段将始终为 null 并且视图状态内容将毫无意义。

您可以将记录计数保留在 ASP.NET 会话状态中,您可以从当前 HTTP 上下文访问该状态:

public int? TotalRecords
{
    get {
        return (int?) HttpContext.Current.Session["TotalRecords"];
    }
    set {
        HttpContext.Current.Session["TotalRecords"] = value;
    }
}

That's because you're trying to use SearchResults as both a Page and the underlying class backing up the ObjectDataSource.

As explained in the documentation, since your data object methods are not static, the ObjectDataSource will create a new instance of the SearchResults class and call the methods on that instance. This means the hidden field will always be null 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:

public int? TotalRecords
{
    get {
        return (int?) HttpContext.Current.Session["TotalRecords"];
    }
    set {
        HttpContext.Current.Session["TotalRecords"] = value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文