Determining which checkbox is checked in the checkbox list

I have an asp.net page that uses a select checkbox (like 10 checkboxes) ... for example

I have enabled AutoPostBack for any changes to the checkboxlist.

First are selected from 10, 3. Also, if the user checks another checkbox, how do you know which particular checkbox was checked by the user and get its value?

Thanks.

-1


source to share


2 answers


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;
        }
    }
}

      



+2


source


If you want to know what was the last checkbox last selected on the server side, you must enable AutoPostBack for each checkbox and capture the values ​​accordingly. If you have flexibility to find out the last checkbox click on the client side then you have to implement a javascript "onclick" event for each checkbox to capture the value in each checkbox and just update the checked value in the hidden variable and pass it back to the server by reverse transmission



-1


source







All Articles