Jas*_*ord 7 asp.net data-binding gridview objectdatasource
我有一个GridView,它有一个指向ObjectDataSource的DataSourceID.ObjectDataSource指向一个方法,该方法使用ObjectDataSource控件的TypeName,SelectMethod和SelectCountMethod属性返回LINQ IQueryable.发生的事情是数据在前期正确加载.然而,在回发,如果我在GridView中删除行并尝试使用明确GridView.DataBind()重新绑定,这是行不通的.我知道LINQ正在返回正确的rowcount,因为我调用了countmethod并返回了正确的rowcount.这是一个简单的例子:
<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" />
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个按钮并添加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();
}
Run Code Online (Sandbox Code Playgroud)
Spr*_*tar 13
网格视图不会在回发时重新绑定,它们的行将从视图状态撤回.在页面加载(或init?)上将gridview的DatasourceID重置为对象数据源ID将导致gridview被反弹.
在查看了更多后面的代码后,我偶然发现页面属性值存储在 ViewState 中。一旦我将其更改为会话,它们就可以工作。
| 归档时间: |
|
| 查看次数: |
25229 次 |
| 最近记录: |