启用分页时,Gridview 在回发时消失

发布于 2024-07-14 07:14:00 字数 301 浏览 9 评论 0原文

我有一个 gridview,其 DataSourceID 属性设置为自定义 ObjectDataSource 对象。 当AllowPaging 设置为True 时,GridView 在回发后消失。 如果我将AllowPaging 设置为False 就可以了。 有人可以帮我解释一下吗? :)

编辑:我感到困惑的另一件事是,我认为如果您设置DataSourceID,网格就会在需要时从数据源获取数据。 如果网格因为不保存数据而消失,为什么 gridview 不从数据源获取所需的数据?

I have a gridview that has its DataSourceID property set to a custom ObjectDataSource object. When AllowPaging is set to True, the GridView disappears after a postback. If I set AllowPaging to False it's fine. Can someone shed some light on this for me? :)

Edit: The other thing I'm confused about is I thought that if you set the DataSourceID that the grid would get data from the datasource whenever it needed it. If the grid is disappearing because it's not holding the data, why isn't the gridview getting the data it needs from the datasource?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赴月观长安 2024-07-21 07:14:00

回发发生后,数据源可能不会被维护或重新填充,并且没有项目可以填充网格。 启用分页时,您是否正确处理数据源对象的状态(重新绑定/保持源活动)?

这听起来像是一个模糊的答案,但如果没有源如何获取数据的示例,就很难诊断为什么这些项目会消失。

编辑:
我想到的方法是回调分页/排序。 不过,我确实找到了一些有关 ODS 和 ODS 的信息。 Paging..确保您已设置以下内容:

  1. GridView:AllowPaging,当然您需要设置PageSize。
  2. ObjectDataSource:EnablePaging,还需要设置:
    • MaximumRowsParameterName="maxRows"
    • StartRowIndexParameterName="startRowIndex"
    • SelectCountMethod="RecordCount"

我认为如果你想手动处理分页大小等,你只需要设置第2项的3个子项。

然后您可以在此处阅读更多内容。

It's possible that after the postback occurs the datasource is not being maintained or refilled and there are no items to populate the grid. Are you handling state correctly for the datasource object (rebinding/keeping the source alive) when paging is enabled?

This may sound like a vague answer, but without an example of how the source is getting the data it's kind of hard to diagnose why the items would be gone.

Edit:
The method I was thinking of was for callback paging/sorting. However I did find some info ont he ODS & Paging.. make sure you have set the following:

  1. GridView: AllowPaging and off course you need to set PageSize.
  2. ObjectDataSource: EnablePaging, also you need to set the:
    • MaximumRowsParameterName="maxRows"
    • StartRowIndexParameterName="startRowIndex"
    • SelectCountMethod="RecordCount"

I think you only need to set the 3 sub items of item 2 if you want to handle paging size etc manually.

Then you can read more up on this here.

素罗衫 2024-07-21 07:14:00

为了回答@adam0101的更多回应,他所说的“......事实证明我的自定义数据源返回记录计数为零。”是什么意思?您可能需要将数据源“重新附加”到网格视图。 ASP.net 自动知道您正在检索记录“n”,其中“n”是下一页的下一个第一行。 此解决方案更适合您不想允许 ASP.NET 在 gridview 中自动处理数据的情况,可能是因为想要在另一个事件(例如“加载数据”按钮)之后加载数据,而不是在页面第一次加载时。 但正如 Adam 在下面提到的,真正的原因是他“...创建了一个继承 ObjectDataSource 的数据源子类,但它的实现不正确。”。 抱歉我的假设是亚当。 谢谢

但是,我认为我的解决方案的其余部分将适用于那些坚持使用手动派生的数据源的人。

按照上面的方式设置 GridView,并注意 - 没有 DataSource 属性! 这是我的示例:

     <asp:GridView ID="gvStudents" DataKeyNames="StudentID" runat="server" 
            ShowFooter="True" AutoGenerateColumns="False" Width="100%" AllowSorting="True" AllowPaging="true" PageSize="10" OnPageIndexChanging="gvStudents_PageIndexChanging">

接下来创建一个私有方法或例程,当您需要将 gridview 强制到新页面时可以调用它。

private void BindGridViewServer(GridView gv1)
{
    gv1.DataSource = sdsStudents;   //re-attach the datasource
    gv1.DataBind();                 //get a page of data AllowPaging must be true
}

接下来,创建 OnPageIndexChanging 方法:

protected void gvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView myGV = (GridView)sender;
    myGV.PageIndex = e.NewPageIndex;
    BindGridViewServer(myGV);
}

为了完成这个答案,这是我的代码,我在需要时加载数据 - 而不是在触发 PostPack 时加载数据,这是默认情况......

    protected void btnSEARCH(object sender, EventArgs e)
        {
//some code
                    //bind the gridview to the datasource here and then bind!
                    gvStudents.DataSource = sdsStudents;
                    gvStudents.DataBind();
//more code etc
        }

To answer more of @adam0101's response, what I think he means by "...It turns out my custom datasource was returning zero for the record count.", is that you may need to "re-attach" the datasource to the gridview. ASP.net automatically knows you're retriving record-'n', where 'n' is the next first row of the next page. This solution is more for cases where you don't want to allow asp.net to automatically handle data in your gridview, perhaps due to wanting to load data after another event (a LOAD DATA button for example), and not when the page has loaded for the first time. But as Adam mentioned below the real reason was he "...had created a datasource subclass that inherited ObjectDataSource, but it was implemented incorrectly.". Sorry for my assumption there Adam. Thanks

However, the rest of my solution I think will work for those stuck on using manually derived data sources.

ie

Set your GridView as per above, and note - without a DataSource property! Here is my example:

     <asp:GridView ID="gvStudents" DataKeyNames="StudentID" runat="server" 
            ShowFooter="True" AutoGenerateColumns="False" Width="100%" AllowSorting="True" AllowPaging="true" PageSize="10" OnPageIndexChanging="gvStudents_PageIndexChanging">

Next make a private method or routine that you can call upon when ever you need to force the gridview onto a new page.

private void BindGridViewServer(GridView gv1)
{
    gv1.DataSource = sdsStudents;   //re-attach the datasource
    gv1.DataBind();                 //get a page of data AllowPaging must be true
}

Next, create the OnPageIndexChanging method:

protected void gvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView myGV = (GridView)sender;
    myGV.PageIndex = e.NewPageIndex;
    BindGridViewServer(myGV);
}

For the sake of completing this answer, here is my code where I load the data when I want - not when a PostPack is fired, which is the default...

    protected void btnSEARCH(object sender, EventArgs e)
        {
//some code
                    //bind the gridview to the datasource here and then bind!
                    gvStudents.DataSource = sdsStudents;
                    gvStudents.DataBind();
//more code etc
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文