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

    技术2022-05-11  40

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

     1 protected   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();1516        for (int i = 0; i < CheckBoxList1.Items.Count; i++)17        {18            //第一种情况19            //原来没有选中 当前却选中 20            //则本次点击操作是:选中 并且点选的是这一个Item21            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            else31            {32                //第二种情况33                //原来有选中 当前却没选中 34                //则本次点击操作是:取消选中 并且点选的是这一个Item35                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        }4344        //保存这次的所有选中的值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


    最新回复(0)