This example mainly solves
List controls such as CheckBoxList cannot directly obtain the current operation Item when the SelectedIndexChanged event is raised.
And which operation type is selected? Or uncheck?
----------
The sample code is as follows:
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 //Bind CheckBoxList operation
6 this.hidtxt_CheckBoxSelectValue.Value = "";//The first time the CheckBoxList is bound
7}
8}
9
10 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
11 {
12 //hidtxt_CheckBoxSelectValue stores the last click value
13 //If it was Page_Load last time, then hidtxt_CheckBoxSelectValue is empty
14 string sOld = this.hidtxt_CheckBoxSelectValue.Value.Trim();
15
16 for (int i = 0; i < CheckBoxList1.Items.Count; i++)
17 {
18 //The first situation
19 //It was not selected originally but is currently selected.
20 //Then this click operation is: select and click on this Item
21 if (CheckBoxList1.Items[i].Selected)
twenty two {
23 if (!sOld.Contains(CheckBoxList1.Items[i].Value.Trim() + ","))
twenty four {
25 //Perform relevant processing
26 Response.Write("This is a selection operation, the Text value of the CheckBox operated is " + CheckBoxList1.Items[i].Text + "The Value value is " + CheckBoxList1.Items[i].Value);
27 i = CheckBoxList1.Items.Count;
28 }
29 }
30 else
31 {
32 //Second case
33 //It was originally selected but is not currently selected.
34 //The click operation this time is: uncheck and click on this Item
35 if (sOld.Contains(CheckBoxList1.Items[i].Value.Trim() + ","))
36 {
37 //Perform related processing
38 Response.Write("This is a deselect operation, the Text value of the CheckBox operated is " + CheckBoxList1.Items[i].Text + "The Value value is " + CheckBoxList1.Items[i].Value);
39 i = CheckBoxList1.Items.Count;
40}
41 }
42 }
43
44 //Save all selected values this time
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;//Prepare for the next comparison
52 }
http://www.cnblogs.com/freeliver54/archive/2007/01/11/617988.html