我尝试通过一些示例来了解在CheckBoxes
分页时如何使GridView
保持选中状态。
我试图通过GridView
复制一个分页示例。我使用以下示例:
PostgreSQL - fetch the row which has the Max value for a column
唯一的区别是我使用自己的GridView如下:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="GridViewExample._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="gvCustomers" DataKeyNames="Key" PageSize="4" AllowPaging="true" OnPageIndexChanging="gvCustomersPageChanging" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked="false" Value='<%#Eval("Key") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Key">
<ItemTemplate>
<%# Eval("Key") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<%# Eval("FirstName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<%# Eval("LastName") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
这是数据加载逻辑:
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
var customers = new List<Customer>{
new Customer { FirstName = "Mohammad", LastName = "Azam", Key = 1},
new Customer { FirstName = "John", LastName = "Doe", Key = 2},
new Customer { FirstName = "Mary", LastName = "Kate", Key = 3},
new Customer { FirstName = "Jerry", LastName = "Seinfeld", Key = 4},
new Customer { FirstName = "Alex", LastName = "Rowe", Key = 5},
new Customer { FirstName = "James", LastName = "Smith", Key = 9},
new Customer { FirstName = "Jerry", LastName = "Grand", Key = 10},
new Customer { FirstName = "Thomas", LastName = "Anders", Key = 11},
new Customer { FirstName = "Diter", LastName = "Bolen", Key = 12},
new Customer { FirstName = "Ryan", LastName = "McDonag", Key = 13},
new Customer { FirstName = "Stewart", LastName = "Black", Key = 14},
new Customer { FirstName = "James", LastName = "Soth", Key = 15},
new Customer { FirstName = "Alex", LastName = "Bowldwin", Key = 18},
new Customer { FirstName = "Arnold", LastName = "Strong", Key = 19},
};
gvCustomers.DataSource = (from c in customers select c).ToList();
gvCustomers.DataBind();
}
这是示例中的代码:
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in gvCustomers.Rows)
{
index = (int)gvCustomers.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;
string id = ((CheckBox)row.FindControl("chkSelect")).ClientID;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
int index = (int)gvCustomers.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
myCheckBox.Checked = true;
}
}
}
}
protected void gvCustomersPageChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
gvCustomers.PageIndex = e.NewPageIndex;
BindData();
RePopulateValues();
}
问题是即使我在调试以下行时选中了复选框:
bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;
在RememberOldValues()
方法中,result
始终为假
为什么?有什么想法吗?
答案 0 :(得分:0)
在Page_Load事件中,请将代码替换为下面的代码。
每次您浏览gridview时,页面都会回发并在达到PageIndexChanging
事件之前重新绑定它,因为您始终在Page_Load
事件中绑定gridview,这会将复选框重置为默认值虚假价值。使用下面的代码,它将永远不会在达到PageIndexChanging
事件之前重新绑定,并且您的复选框值将被保留。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}