DataList分页,保存CheckBox控件状态

1、DataList中嵌套CheckBox

<asp:DataList ID="dlNews" runat="server">                
   <HeaderTemplate>
     <table cellpadding="0" cellspacing="0"><tr><td align="center">复选框</td></tr></table>
   </HeaderTemplate>
   <ItemTemplate>
     <table cellpadding="0" cellspacing="0"><tr><td align="center">
       <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
     </td></tr></table>
   </ItemTemplate>
   <HeaderStyle HorizontalAlign="Center" />
</asp:DataList>

2、CheckBox1_CheckedChanged函数

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    RememberOldValues();
}

3、RememberOldValues()函数
using System.Collections;

public void RememberOldValues()
{
    ArrayList categoryIDList = new ArrayList();
    if (Session[subTable] != null)
    {
        categoryIDList = (ArrayList)Session[subTable];
    }
    int index = 0;
    foreach (DataListItem item in dlNews.Items)
    {
        index = item.ItemIndex + (Int32.Parse(labPage.Text) - 1) * 7;
        bool result = ((CheckBox)item.FindControl("CheckBox1")).Checked;
        if (result)
        {
            if (!categoryIDList.Contains(index))
            {
                categoryIDList.Add(index);
            }
        }
        else
        {
            categoryIDList.Remove(index);
        }
    }
    if (categoryIDList != null && categoryIDList.Count > 0)
    {
        Session[subTable] = categoryIDList;
    }
}

其中:labPage.Text为当前页,数字“7”为一页显示的数量

4、复现保存的选择结果
private void RePopulateValues()
{
    ArrayList categoryIDList = (ArrayList)Session[subTable];
    if (categoryIDList != null && categoryIDList.Count > 0)
    {
        foreach (DataListItem item in dlNews.Items)
        {
            int index = item.ItemIndex;
            if (categoryIDList.Contains(index + (Int32.Parse(labPage.Text) - 1) * 7))
            {
                CheckBox myCheckBox = (CheckBox)item.FindControl("CheckBox1");
                myCheckBox.Checked = true;
            }
        }
    }
}
此函数可以放在DataList的数据绑定函数中bind()
protected void bind()
{
    ......
    RePopulateValues();
}

参考:
http://zxianf.blog.163.com/blog/static/301207012010101112317730/
Asp.Net获取Session、Cookies、Application中值:
http://www.cnblogs.com/ZHF/archive/2009/11/12/1601486.html

转载于:https://my.oschina.net/sxq0714/blog/63243

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值