Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

For CheckBoxList, i am having a problem handling SelectedIndexChanged event. When i handle SelectedIndexChanged event, i can't reference the checkbox that was clicked ( checked or Unchecked Status ) , as the "SelectedIndex" only references the first checked checkbox. I want this, since I have to do something like :
 If ( Current checkbox Status = Checked )
{
    do something
}
elseif(Current checkbox Status = UnChecked )
{
   do something
} 


Is there a way to find, (1) SelectedValue / SelectedIndex of Current CheckBox(ticked/unticked) in CheckBoxList. (2) To get the Status(Checked /UnChecked ) of the current Checkbox(ticked/unticked) on the SelectedIndexChangeProperty()?

Thanks In Advance,
Posted
Updated 29-Sep-10 0:39am
v5

I used the following sample code to try to get it done:

Markup :

XML
<asp:CheckBoxList ID="CheckBoxList1" AutoPostBack="true" runat="server"
       onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
   </asp:CheckBoxList>


CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            ListItem item1 = new ListItem("1", "1");
            ListItem item2 = new ListItem("2", "2");
            ListItem item3 = new ListItem("3", "3");

            CheckBoxList1.Items.Add(item1);
            CheckBoxList1.Items.Add(item2);
            CheckBoxList1.Items.Add(item3);
        }
    }
 
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckBoxList1.SelectedItem != null)
        {
            if (CheckBoxList1.SelectedItem.Selected)
                Response.Write("You checked " + CheckBoxList1.SelectedItem.Text);
            else
                Response.Write("You unchecked " + CheckBoxList1.SelectedItem.Text);
        }
}


The SelectedIndexChanged event is fired in both cases (Check box check and uncheck) and, Asp.net can give you the correct selected item in the CheckBoxList when you "check" one (I found one case however it was failing to show the correct check box). But, unfortunately, when you "uncheck" a check box, it can't give you the information because the "CheckBoxList1.SelectedItem" is null and there is no other suitable property to get this information.

So, you have to do some client side scripting (Using JQuery or JavaScripts) to get the checked or unchecked check box reference, store or send this information to codebehind and then execute the server side code with that information.
 
Share this answer
 
Comments
shwetavc30 29-Sep-10 5:24am    
this isnt helping ... can u provide javascript code. how to call it from code behind ?
use

SQL
if (CheckBoxList1.Items[i].Selected == true)
    {
    }


in stead of if (CheckBoxList1.SelectedItem != null)

this will be in a for loop

like

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{}


Hope this will be helpful for you.
 
Share this answer
 
v2
Comments
NaveenSoftwares 19-Sep-12 9:08am    
I dont think there is a option called "CheckBoxList1.Items[i].Selected"
arindamrudra 31-Oct-12 14:19pm    
@NaveenSoftwares - Can you please follow this link http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist(v=vs.100).aspx . Hope This will help you to learn new things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900