In this article I will explain with an example, how to populate TextBox based on DropDownList selection in ASP.Net using C# and VB.Net.
The selected value of the DropDownList will be displayed in TextBox and Label controls.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
HTML Markup
The following HTML Markup consists of an ASP.Net DropDownList, a Label and a TextBox control.
The DropDownList has been assigned OnSelectedIndexChanged event and also the AutoPostBackproperty is set to True.
<form id="form1" runat="server">
<div>
Select Customer ID
<asp:DropDownList ID="ddlCustomers" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<br />
<p>---Customer Details---</p>
<hr />
City :
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br />
Country :
<asp:Label ID="lblCountry" runat="server" Text=""></asp:Label>
</div>
</form>
Populating DropDownList from Database
Inside the Page Load event of the page, the DropDownList is populated with records from the Customers table of the Northwind database.
A Select Query is executed and it gets the CustomerID and the ContactName fields. The CustomerID field is assigned to the DataValueField property of the DropDownList and the ContactName field is assigned to the DataTextField property.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCustomers.Items.Add(new ListItem("--Select Customer--", ""));
ddlCustomers.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select CustomerID, ContactName from Customers";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "ContactName";
ddlCustomers.DataValueField = "CustomerID";
ddlCustomers.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlCustomers.Items.Add(New ListItem("--Select Customer--", ""))
ddlCustomers.AppendDataBoundItems = True
Dim strConnString As String = ConfigurationManager _
.ConnectionStrings("conString").ConnectionString
Dim strQuery As String = "select CustomerID, ContactName from Customers"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "ContactName"
ddlCustomers.DataValueField = "CustomerID"
ddlCustomers.DataBind()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End If
End Sub
Populating TextBox and Label controls on DropDownList selection
Inside the OnSelectedIndexChanged event handler, the City and Country fields are fetched based on the selected CustomerID value.
The fetched values are then displayed in the TextBox and Label controls respectively.
C#
protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select City, Country from Customers where" +
" CustomerID = @CustomerID";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@CustomerID", ddlCustomers.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
SqlDataReader sdr= cmd.ExecuteReader();
while (sdr.Read())
{
txtCity.Text = sdr[0].ToString();
lblCountry.Text = sdr["Country"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
VB.Net
Protected Sub ddlCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As String = ConfigurationManager _
.ConnectionStrings("conString").ConnectionString
Dim strQuery As String = "select City, Country from Customers where" _
& " CustomerID = @CustomerID"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Parameters.AddWithValue("@CustomerID", ddlCustomers.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
txtCity.Text = sdr(0).ToString()
lblCountry.Text = sdr("Country").ToString()
End While
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
Screenshot
Demo
Downloads