我有一个GridView,它有一个指向ObjectDataSource的DataSourceID。 ObjectDataSource指向通过使用ObjectDataSource控件的TypeName,SelectMethod和SelectCountMethod属性返回LINQ IQueryable的方法。会发生什么情况是数据会正确加载。但是,在回发时,如果我从GridView中删除行并尝试使用显式的GridView.DataBind()重新绑定,则不起作用。我知道LINQ正在返回适当的rowcount,因为我已经调用了countmethod方法,并且它返回了适当的rowcount。这里有一个简单的例子:GridView在回发后没有正确地重新绑定
<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事件中。无论我尝试什么,它都不起作用。
正如有人建议下面,我试着将它移动到Page_Load以及,没有去。这里是我的代码一个粗略的例子:
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();
}
再次修改上述内容。 – 2009-11-20 14:30:39