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

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

问题描述

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

 < asp:GridView ID =TestGridViewrunat =serverPageSize =20
AutoGenerateColumns =falseAllowPaging =true
AllowSorting =falseDataSourceID =TestDataSource>
<列>
...
< /列>
< / asp:GridView>

< asp:ObjectDataSource ID =TestDataSourcerunat =server
EnablePaging =trueSelectCountMethod =GetDetailCount
SelectMethod =GetDetailsTypeName =MyApp .PageClass/>

我尝试添加一个按钮并添加TestGridView.DataBind();方法。我试着将它添加到Page_PreRender事件中。无论我尝试什么,它都无法正常工作。



正如有人在下面建议的,我试着将它移动到Page_Load中,也没有去。下面是我的代码的一个简单例子:

$ p $ protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
//设置初始查询参数,然后...
BindData();



private void BindData()
{
// EDITED:删除了下面的代码,因为我不想删除
//来自数据库的行,而是让GridView重新绑定
//而不是它。
////从GridView中移除所有当前行
// int colCount = TestGridView.Rows.Count;
// for(int x = 1; x <= colCount; x ++)
// {
// TestGridView.DeleteRow(x);
//}

//将GridView绑定到ObjectDataSource
TestGridView.DataBind();
}

protected void RegenerateImageButton_Click(object sender,ImageClickEventArgs e)
{
//设置updated查询参数,然后...
BindData ();
}


解决方案

多一点,我偶然发现存储在ViewState中的页面属性值。一旦我将其更改为Session,它们就可以工作。


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" />

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.

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();
}

解决方案

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天全站免登陆