ASP.Net DropDownList OnSelectedIndexChanged Event

Updated on 04 Jul 2012,
Published on 28 Oct 2009

The OnSelectedIndexChanged event handler of ASP.Net DropDownList control enables you to handle the click event of list item at server side that is raised at the time when user clicks to select a list item of dropdownlist control. The SelectedIndexChanged event of DropDownList control occurs only if the AutoPostBack property of dropdownlist is specified with value "true". The SelectedIndexChanged event occurs immediately when the index of the selected list item of the dropdown list item changes and it submits the web form to the server only if its 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 based on the value of select item of the dropdownlist menu.

Sample Code for ASP.Net DropDwonList OnSelectedIndexChanged Event

HTML Code
<asp:DropDownList ID="DropDownList1" 
    runat="server" 
    AutoPostBack="true" 
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Value="item 1"></asp:ListItem>
    <asp:ListItem Value="item 2"></asp:ListItem>
    <asp:ListItem Value="item 3"></asp:ListItem>
    <asp:ListItem Value="item 4"></asp:ListItem>
    <asp:ListItem Value="item 5"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

In the above sample code for DropDownList control we have specified the AutoPostBack property value as "true" and also OnSelectedIndexChanged server side event is used to handle the click event of dropdown list item when user clicks an item to select it.

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

1. Double clicking the dropdownlist 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 dropdownlist 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)
{
    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}

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

When you will write the ID of the dropdownlist control inside the Page_Load method for example DropDownList1, and will type the "." Dot operator it will display the list of properties and methods of dropdownlist 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 DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = DropDownList1.SelectedValue;
}

In the above C# sample code for SelectIndexChanged event handler of ASP.Net DropDownList control we have used the SelectedValue property to get the value of the select item. When user will click on any list item of dropdownlist menu it will raise the SelectedIndexChanged event that will execute the above specified code to get the value of selected item.

Output:

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

DropDownList Control OnSelectedIndexChanged Event

Continue to next tutorial: ASP.Net DropDownList SQL Datasource Databinding to learn how to display the data retrieved from the SQL database as list items of dropdownlist control.

5 Responses to "ASP.Net DropDownList OnSelectedIndexChanged Event"
Rick Svitak
It's those simple things that get ya ...

Thanks
sajid anwer
this is very good document, by this document my problem solved
Dhir
Hi, Please any one tell me how can I select item form DropDownList Asp.Net(C#). I have populated dropdownitems from my database. but the select index changed is not working stuck from the whole day.
Ezineasp.net
@Dhir Hi, Please check your page_load event, seems like you have placed your dropdownlist control's databinding code at page load without applying any postback check. Can you check it and reply back!
Dhir
@Ezineasp.net Hi that's the problem I forgot page.ispostback now code is working fine thanks ...
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email