ASP.Net CheckBoxList OnSelectedIndexChanged Event

Updated on 03 Jul 2012,
Published on 21 Oct 2009

The OnSelectedIndexChanged event handler of ASP.Net CheckBoxList control enables you to handle the click event of checkboxlist item at server side that is raised at the time when user clicks the checkbox list item to change its checked state. The SelectedIndexChanged event of CheckboxList control occurs only if the AutoPostBack property of checkboxlist is specified with value "true". The SelectedIndexChanged event occurs immediately when the checked state of the checkbox list item changes and it submits the web form to the server only if the AutoPostBack property has value "true". You can handle the event at server side using C# code and apply the custom logic there to execute any specific action.

Sample Code for ASP.Net CheckBoxList OnSelectedIndexChanged Event

<asp:CheckBoxList ID="CheckBoxList1" 
    <asp:ListItem Value="Item 1">Item 1</asp:ListItem>
    <asp:ListItem Value="Item 2">Item 2</asp:ListItem>
    <asp:ListItem Value="Item 3">Item 3</asp:ListItem>
    <asp:ListItem Value="Item 4">Item 4</asp:ListItem>
    <asp:ListItem Value="Item 5">Item 5</asp:ListItem>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

In the above sample code for CheckboxList control we have specified the AutoPostBack property value as "true" and also OnSelectedIndexChanged server side event is used to handle the click event of checkbox list item when user clicks the control to change its checked state.

You can attach the SelectedIndexChanged of checkboxlist control in two ways as follows:

1. Double clicking the checkboxlist control for which you want to generate the server side OnSelectedIndexChanged event handler. In this tutorial we have also used the same approach to handle the SelectedIndexChanged event of checkboxlist control.

2. You can also attach the SelectedIndexChanged event programmatically in the page load method of ASP.Net web page E.g.:

protected void Page_Load(object sender, EventArgs e)
    CheckBoxList1.SelectedIndexChanged += new EventHandler(CheckBoxList1_SelectedIndexChanged);

void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    throw new Exception("The method or operation is not implemented.");

When you will write the ID of the checkboxlist control inside the Page_Load method for example CheckBoxList1, and will type the "." Dot operator it will display the list of properties and methods of checkboxlist control. Select the SelectedIndexChanged event from the intellisense and press "+=" and then tab key from the keyboard. It will generate the event handler automatically.

C# code
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    Label1.Text = CheckBoxList1.SelectedIndex.ToString();

    if (CheckBoxList1.SelectedIndex != -1)
        Label1.Text = CheckBoxList1.SelectedItem.Value;

In the above C# sample code for SelectIndexChanged event handler of ASP.Net CheckBoxList control we have used the "if condition" to check whether it has any item in checked state or not. The SelectedIndex property of CheckBoxList control returns "-1" if there is no list item in checked state which means there is checkbox list item selected by the user. You can write your logic inside the "if condition" block to execute the handler without throwing any error or exception.


Get the output for above discussed code from the following link:

CheckBoxList Control OnSelectedIndexChanged Event

Continue to next tutorial: ASP.Net CheckBoxList Control with SQL DataSource DataBinding to learn how to display the data retrieved from the SQL database as list items of checkboxlist control.

3 Responses to "ASP.Net CheckBoxList OnSelectedIndexChanged Event"
Hi, Question. When we check two or more items of the checkBoxList, the selectedValue that you print in the label1 is the first checked item , so if i need information about the last checked item , How can i do? Best regards!
@Sandy You can learn how to get the values of all the selected items from the following tutorial: Hope this will work for you!
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email