原理很简单,利用CheckBox的属性cid来取选中的值,在代码页保存到页面ArrayList类型的ViewState变量中,在分页时触发事件对选中的值进行还原。
客户端代码
1
<
asp:GridView
ID
="gvItems"
runat
="server"
>
2 < Columns >
3 < asp:TemplateField HeaderText ="选项" >
4 < ItemTemplate >
5 < asp:CheckBox ID ="rbtnName" runat ="server" cid ='<%# Eval("CID") % > '
6 oncheckedchanged="rbtnName_CheckedChanged"/>
7 </ ItemTemplate >
8 </ asp:TemplateField >
9 </ Columns >
10 </ asp:GridView >
11 < AspNetPager ID ="anpPage" runat ="server" PageSize ="8" OnPageChanged ="anpResume_PageChanged" >
12 </ AspNetPager >
2 < Columns >
3 < asp:TemplateField HeaderText ="选项" >
4 < ItemTemplate >
5 < asp:CheckBox ID ="rbtnName" runat ="server" cid ='<%# Eval("CID") % > '
6 oncheckedchanged="rbtnName_CheckedChanged"/>
7 </ ItemTemplate >
8 </ asp:TemplateField >
9 </ Columns >
10 </ asp:GridView >
11 < AspNetPager ID ="anpPage" runat ="server" PageSize ="8" OnPageChanged ="anpResume_PageChanged" >
12 </ AspNetPager >
代码页
1
//
声明保存选项的变量
2 ArrayList alItems
3 {
4 set { ViewState[ " alItems " ] = value; }
5 get { return (ArrayList)ViewState[ " alItems " ]; }
6 }
7
8 // 实例化这个变量
9 protected void Page_Load( object sender, EventArgs e)
10 {
11 if ( ! IsPostBack)
12 {
13 alItems = new ArrayList();
14 }
15 }
16
17 #region 选择题目名称时发生的事件,将选中的值保存到ArrayList数组中
2 ArrayList alItems
3 {
4 set { ViewState[ " alItems " ] = value; }
5 get { return (ArrayList)ViewState[ " alItems " ]; }
6 }
7
8 // 实例化这个变量
9 protected void Page_Load( object sender, EventArgs e)
10 {
11 if ( ! IsPostBack)
12 {
13 alItems = new ArrayList();
14 }
15 }
16
17 #region 选择题目名称时发生的事件,将选中的值保存到ArrayList数组中
18
protected
void
rbtnName_CheckedChanged(
object
sender, EventArgs e)
19 {
20 CheckBox cb = (CheckBox)sender;
21 int subID = Convert.ToInt32(cb.Attributes[ " cid " ]);
22
23 if (cb.Checked == true )
24 {
25 alItems.Add(subID);
26 }
27 else
28 {
29 alItems.Remove(subID);
30 }
31 }
32 #endregion
33
34 #region 绑定Check列表,用于分页和编辑时的视图状态保存
35 private void bindCheckedByList()
36 {
37 for ( int i = 0 ; i <= gvItems.Rows.Count - 1 ; i ++ )
38 {
39 CheckBox cb = (CheckBox)gvItems.Rows[i].FindControl( " rbtnName " );
40 int subID = Convert.ToInt32(cb.Attributes[ " cid " ]);
41
42 if (alItems.Contains(subID))
43 {
44 cb.Checked = true ;
45 }
46 }
47 }
48 #endregion
49
50 // 分页
51 protected void anpResume_PageChanged( object sender, EventArgs e)
52 {
53 BindData(); // 绑定GridView数据的方法
54 bindCheckedByList(); // 绑定Check列表
55 }
56
19 {
20 CheckBox cb = (CheckBox)sender;
21 int subID = Convert.ToInt32(cb.Attributes[ " cid " ]);
22
23 if (cb.Checked == true )
24 {
25 alItems.Add(subID);
26 }
27 else
28 {
29 alItems.Remove(subID);
30 }
31 }
32 #endregion
33
34 #region 绑定Check列表,用于分页和编辑时的视图状态保存
35 private void bindCheckedByList()
36 {
37 for ( int i = 0 ; i <= gvItems.Rows.Count - 1 ; i ++ )
38 {
39 CheckBox cb = (CheckBox)gvItems.Rows[i].FindControl( " rbtnName " );
40 int subID = Convert.ToInt32(cb.Attributes[ " cid " ]);
41
42 if (alItems.Contains(subID))
43 {
44 cb.Checked = true ;
45 }
46 }
47 }
48 #endregion
49
50 // 分页
51 protected void anpResume_PageChanged( object sender, EventArgs e)
52 {
53 BindData(); // 绑定GridView数据的方法
54 bindCheckedByList(); // 绑定Check列表
55 }
56