Click here to Skip to main content
11,576,293 members (63,875 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 27-Sep-10 18:28pm
Edited 29-Sep-10 0:39am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

Markup :

<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.
  Permalink  
Comments
shweta.chikhalikar at 29-Sep-10 5:24am
   
this isnt helping ... can u provide javascript code. how to call it from code behind ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

use

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.
  Permalink  
v2
Comments
NaveenSoftwares at 19-Sep-12 9:08am
   
I dont think there is a option called "CheckBoxList1.Items[i].Selected"
arindamrudra at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 285
1 F-ES Sitecore 140
2 Abhinav S 115
3 Lucifier Rocks 105
4 OriginalGriff 105
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 630
2 Abhinav S 438
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 29 Sep 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100