回发后 GridView 未正确重新绑定 [英] GridView not Rebinding Properly After Postback

查看:34
本文介绍了回发后 GridView 未正确重新绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView,它有一个指向 ObjectDataSource 的 DataSourceID.ObjectDataSource 指向一个方法,该方法使用 ObjectDataSource 控件的 TypeName、SelectMethod 和 SelectCountMethod 属性返回 LINQ IQueryable.发生的情况是数据预先正确加载.但是,在回发时,如果我从 GridView 中删除行并尝试使用显式 GridView.DataBind() 重新绑定,则它不起作用.我知道 LINQ 正在返回正确的行数等,因为我已经调用了 countmethod 并且它返回了正确的行数.这是一个简单的例子:

I have a GridView that has a DataSourceID pointing to an ObjectDataSource. The ObjectDataSource points to a method that returns a LINQ IQueryable by using the TypeName, SelectMethod, and SelectCountMethod properties of the ObjectDataSource control. What happens is that the data loads properly upfront. However, on postback, if I remove the rows from the GridView and try to rebind using the explicit GridView.DataBind(), it doesn't work. I know LINQ is returning the proper rowcount and such because I've called the countmethod and it returns the proper rowcount. Here's a quick example:

<asp:GridView ID="TestGridView" runat="server" PageSize="20" 
    AutoGenerateColumns="false" AllowPaging="true" 
    AllowSorting="false" DataSourceID="TestDataSource">
    <Columns>
        ...
    </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="TestDataSource" runat="server" 
    EnablePaging="true" SelectCountMethod="GetDetailCount" 
    SelectMethod="GetDetails" TypeName="MyApp.PageClass" />

我尝试添加一个按钮并添加 TestGridView.DataBind();方法.我试过将它添加到 Page_PreRender 事件中.无论我尝试什么,它都不起作用.

I've tried adding a button and adding the TestGridView.DataBind(); method to that. I've tried adding it to the Page_PreRender event. No matter what I try, it's not working.

正如下面有人建议的那样,我也尝试将其移至 Page_Load,但没有成功.这是我的代码的粗略示例:

As someone suggested below, I've tried moving it to Page_Load as well, and no go. Here's a rough example of my code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set "initial" query parameters, then ...
        BindData();
    }
}

private void BindData()
{
    // EDITED: Removed the code below since I'm not looking to delete the
    //         rows from the database, but rather get the GridView to rebind
    //         which its not.
    ////Remove all current rows from the GridView
    //int colCount = TestGridView.Rows.Count;
    //for (int x = 1; x <= colCount; x++)
    //{
    //    TestGridView.DeleteRow(x);
    //}

    // Bind GridView to the ObjectDataSource
    TestGridView.DataBind();
}

protected void RegenerateImageButton_Click(object sender, ImageClickEventArgs e)
{
    // Set "updated" query parameters, then ...
    BindData();
}

推荐答案

在查看更多背后的代码后,我偶然发现了存储在 ViewState 中的页面属性值.一旦我将其更改为 Session,它们就可以工作了.

After looking at the code behind a bit more, I stumbled across page property values being stored in ViewState. Once I changed it over to Session, they work.

这篇关于回发后 GridView 未正确重新绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆