Bind A DropDownList With Another DropDownList In ASP.NET Web Forms
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace BindingDDL
{
public partial class WebForm1 : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountryDDL();
}
}
void BindCountryDDL()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from country";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataTable data = new DataTable();
sda.Fill(data);
CountryDropDownList.DataSource = data;
CountryDropDownList.DataTextField = "country_name";
CountryDropDownList.DataValueField = "country_id";
CountryDropDownList.DataBind();
ListItem selectItem = new ListItem("Select Country", "Select Country");
selectItem.Selected = true;
CountryDropDownList.Items.Insert(0,selectItem);
}
void BindCityDDL(int country_id)
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from city where c_id = @country_id";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.SelectCommand.Parameters.AddWithValue("@country_id",country_id);
DataTable data = new DataTable();
sda.Fill(data);
CityDropDownList.DataSource = data;
CityDropDownList.DataTextField = "city_name";
CityDropDownList.DataValueField = "city_id";
CityDropDownList.DataBind();
ListItem selectItem = new ListItem("Select City", "Select City");
selectItem.Selected = true;
CityDropDownList.Items.Insert(0, selectItem);
}
protected void CountryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int country_id = Convert.ToInt32(CountryDropDownList.SelectedValue);
BindCityDDL(country_id);
}
catch(Exception ex)
{
Response.Write("<script>alert('Country is Required')</script>");
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
Response.Write("Selected Country is : " + CountryDropDownList.SelectedItem.ToString() + "<br>");
Response.Write("Selected Country Id is : " + CountryDropDownList.SelectedValue.ToString() + "<br>");
Response.Write("Selected City is : " + CityDropDownList.SelectedItem.ToString() + "<br>");
Response.Write("Selected City Id is : " + CityDropDownList.SelectedValue.ToString() + "<br>");
}
}
}
Above File is BindingDDL\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BindingDDL.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 70px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="4" cellspacing="4" class="auto-style1">
<tr>
<td class="auto-style2">Country</td>
<td>
<asp:DropDownList ID="CountryDropDownList" runat="server" AutoPostBack="True" Height="39px" OnSelectedIndexChanged="CountryDropDownList_SelectedIndexChanged" Width="209px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CountryDropDownList" Display="Dynamic" ErrorMessage="Country is Required" ForeColor="Red" InitialValue="Select Country" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">City</td>
<td>
<asp:DropDownList ID="CityDropDownList" runat="server" Height="39px" Width="209px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="CityDropDownList" Display="Dynamic" ErrorMessage="City is Required" ForeColor="Red" InitialValue="Select City" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="SubmitButton" runat="server" OnClick="SubmitButton_Click" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Comments
Post a Comment