借助HiddenText 确定CheckBoxList当前的操作类型及点击的CheckBox

本次示例主要是解决
CheckBoxList这样的List控件
在引发SelectedIndexChanged事件时
本身不能直接得到当前的操作Item
以及是哪种操作类型 选中? 还是 取消选中?
-----------
示例代码如下:

 1protected void Page_Load(object sender, EventArgs e)
 2    {
 3        if (!IsPostBack)
 4        {
 5            //绑定CheckBoxList操作
 6            this.hidtxt_CheckBoxSelectValue.Value = "";//第一次绑定完CheckBoxList
 7        }

 8    }

 9
10    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
11    {
12        //hidtxt_CheckBoxSelectValue 存储的是上次的点选值
13        //如果上次是Page_Load 则hidtxt_CheckBoxSelectValue为空
14        string sOld = this.hidtxt_CheckBoxSelectValue.Value.Trim();
15
16        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
17        {
18            //第一种情况
19            //原来没有选中 当前却选中 
20            //则本次点击操作是:选中 并且点选的是这一个Item
21            if (CheckBoxList1.Items[i].Selected)
22            {
23                if (!sOld.Contains(CheckBoxList1.Items[i].Value.Trim() + ","))
24                {
25                    //进行相关处理
26                    Response.Write("本次是选中操作,操作的CheckBox的Text值是" + CheckBoxList1.Items[i].Text + "其Value值是" + CheckBoxList1.Items[i].Value);
27                    i = CheckBoxList1.Items.Count ;
28                }

29            }

30            else
31            {
32                //第二种情况
33                //原来有选中 当前却没选中 
34                //则本次点击操作是:取消选中 并且点选的是这一个Item
35                if (sOld.Contains(CheckBoxList1.Items[i].Value.Trim() + ","))
36                {
37                    //进行相关处理
38                    Response.Write("本次是取消选中操作,操作的CheckBox的Text值是" + CheckBoxList1.Items[i].Text + "其Value值是" + CheckBoxList1.Items[i].Value);
39                    i = CheckBoxList1.Items.Count;
40                }

41            }

42        }

43
44        //保存这次的所有选中的值
45        string sNew = "";
46        foreach (ListItem item in CheckBoxList1.Items)
47        {
48            if (item.Selected)
49                sNew += " " + item.Value.Trim() + ",";
50        }

51        this.hidtxt_CheckBoxSelectValue.Value = sNew;//为下一次的比较做准备        
52    }

posted on 2007-01-11 17:59  freeliver54  阅读(1111)  评论(1编辑  收藏  举报

导航