借助HiddenText 确定CheckBoxList当前的操作类型及点击的CheckBo
点击次数:20 次 发布日期:2008-11-26 11:36:15 作者:源代码网
|
源代码网推荐 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 } 源代码网推荐 源代码网推荐 http://www.cnblogs.com/freeliver54/archive/2007/01/11/617988.html 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
