ObjectDataSource を指す DataSourceID を持つ GridView があります。ObjectDataSource は、ObjectDataSource コントロールの TypeName、SelectMethod、および SelectCountMethod プロパティを使用して、LINQ IQueryable を返すメソッドを指します。何が起こるかというと、データが事前に適切に読み込まれます。ただし、ポストバック時に、GridView から行を削除し、明示的な GridView.DataBind() を使用して再バインドしようとすると、機能しません。countmethod を呼び出して適切な行数を返すため、LINQ が適切な行数などを返していることはわかっています。簡単な例を次に示します。
<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();
}