Skip to main content
 首页 » 编程设计

c#之与 ASP.net 中的 GridView 相关的问题

2024年02月23日15bhlsheji

我有从数据库绑定(bind)的 gridview..

我有以下代码:

 protected void Page_Load(object sender, EventArgs e) 
    { 
        BindGrid(); 
    } 

现在,我在 gridView 中有 checkBox 和 drop-down,当用户从复选框中选择一些行并点击 Update Button 时, Page_Load 事件触发并调用 BindGrid(); 方法,选定的行应该被隐藏。

如何在页面加载事件后保留复选框值。

我不想在页面加载中使用 IsPostBack 属性,因为我已经使用了 Paging。

我该如何解决我的问题?

请您参考如下方法:

您应该只DataBind GridView if(!Page.IsPostback)。否则,不会触发任何事件,并且 ViewState 值(如 SelectedIndex 等)会被 DataSource 值覆盖。

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback%28v=VS.100%29.aspx

if(!IsPostBack) 
{ 
   BindGrid(); 
} 

您还应该从以下事件处理程序调用 BindGrid:

  • PageIndexChanging
  • SelectedIndexChanged
  • 排序