Get last index of checked value of a checkboxlist

Some of us facing a problem, that how to find last checkedvalue from a CheckboxList. Because if you only use below code,

CheckBoxList1.SelectedValue

to get the last checked value, then unfortunately you will not reach your requirement. Because It will give you the the minimum index, if have selected.
Or you can try to get the result by using ForLoop loop or linq by below code,

Linq:

List<int> SelectedCode = new List<int>();

  //Taking all selected values
  SelectedCode = (from i in CheckBoxList1.Items.Cast<ListItem>() where i.Selected select i.Value.ToInt()).ToList();

Foreach

List<int> SelectedCode = new List<int>();

foreach (ListItem lst in CheckBoxList1.Items)
 {
        if(lst.Selected == true)
{
SelectedCode.Add(lst)
}              
                
 }
 In that you can get all the selected value of checkboxlist, not lst checked value.
So, may be, this small problem can become a big problem in our busy schedule, and can spoil huge time.


Don't worry here is the solution of this problem

string value = string.Empty;

        string result = Request.Form["__EVENTTARGET"];

        string[] checkedBox = result.Split('$'); 

        int index = int.Parse(checkedBox[checkedBox.Length - 1]);

        if (CheckBoxList1.Items[index].Selected)
        {
            value = CheckBoxList1.Items[index].Value;
        }        

Above code is the solution of your problem.
Here the solutions based on catch the output from html code. If you look that code little bit carefully, you can understand the logic behind this.