Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Working with onSelectedIndexChange Event in DropDownList

Author: DevASP Team

In this article I will try to explain you how to use onSelectedIndexChange event in DropDownList control.

This article is about how you can change the selected value of dynamically bind dropdownlist control. To begin with this, create a new application in 2.0 and add the label control and the dropdownlist control. Don’t forget to add the two attributes that are DataTextField and DataValueField. In DataTextField mention the field you want to display in DropDownList control and in DataValueField the value for that text. For example you are binding the dropdownlist control with the customer record so in this case DataTextField can be Customer_Name and its value can be Ticket_id. The main thing you will add is OnSelectedIndexChange attribute which will contain the value of the event which will fire on your index change. After all this your aspx source view would be look like as follows:


<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>

<br /><br />


<asp:DropDownList ID="DropDownList1" DataTextField="Name" DataValueField="TicketID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  runat="server" AutoPostBack="True">          


Create a method which will connect your control with database table and fill that control:


Sub bindDropDown()


Dim objAdap As New SqlDataAdapter("SELECT TicketID, Name from PassengerTicket", objConn)

      Dim dt As New DataTable


      DropDownList1.DataSource = dt



End Sub

Here I have used the datatable but you can use DataSet also to perform the same task. Now on page load event bind the add the following code:


        If Not IsPostBack Then


        End If

And the last step is the event:


Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged


        Label1.Text = DropDownList1.SelectedIndex


End Sub

Article Comments
Very useful walkthrough, I was doing the same but i was missing to check PostBack Property before binding. That caused my dropdownlist to show same value every time I change index.

Posted on 1/24/2010 2:24:37 PM by Sajid Iqbal

very Good Thanks for this site

Posted on 2/4/2010 4:46:09 AM by ramanathan.s

Add Article Comment:
Name :
Email Address :
Comments :
<< How to bind XML file with Repeater Control

Disclaimer - Privacy
© 2002-2017