views:

506

answers:

2

I've a asp.net page which uses multi select checkboxlist(say having 10 checkboxes)....for example

I've enabled AutoPostBack for any change in checkboxlist.

Initially,out of 10, 3 are selected. On top of this, if user checks another checkbox, how do I know which particular checkbox has been checked by the user and retrieve its value?

Thanks.

+1  A: 
for (int i=0; i<checkboxlist1.Items.Count; i++)
{    
    if (checkboxlist1.Items[i].Selected)
    {
    }    
}

protected void Page_Load(object sender, EventArgs e)
{
    string name = Request.Form["__EVENTTARGET"] ?? String.Empty;
    if (name.IndexOf("CheckBoxList1") != -1)
    {
        int last = name.LastIndexOf("$") + 1;
        int index = Convert.ToInt32(name.Substring(last, name.Length - last - 1));
        if (CheckBoxList1.Items[index].Selected)
        {
            string text = CheckBoxList1.Items[index].Text;
            string value = CheckBoxList1.Items[index].Value;
        }
    }
}
Phaedrus
thanks for your inputs..i want to know which last checkbox was clicked by the user?
Steve Chapman
A: 

If you want to know which last checkbox was clicked on the server side, you should enable AutoPostBack for each checkbox and capture the values accordingly. If you have the flexibility to find out the last checkbox click on the client side, then you should implement a javascript "onclick" event for each checkbox to capture the value on each checkbox and simply update the checked value in a hidden variable and pass it back to the server on postback

Vikram