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