我有一個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