Follow us

Header Ads

Bind Country Dropdown to City DropDown in Asp.Net WebForm

 Bindonedropdowntoanotherdropdown.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bindonedropdowntoanotherdropdown.aspx.cs" Inherits="Bindonedropdowntoanotherdropdown" %>

<!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: 167px;
        }
        .auto-style3 {
            width: 167px;
            height: 62px;
        }
        .auto-style4 {
            height: 62px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <table class="auto-style1">
                <tr>
                    <td class="auto-style3">Country</td>
                    <td class="auto-style4">
                        <asp:DropDownList ID="CountryDropDownList" runat="server" AutoPostBack="True" Height="16px" OnSelectedIndexChanged="CountryDropDownList_SelectedIndexChanged" Width="253px">
                        </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="16px" Width="253px">
                        </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">&nbsp;</td>
                    <td>
                        <asp:Button ID="SubmitButton" runat="server" OnClick="SubmitButton_Click" Text="SUBMIT" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 Bindonedropdowntoanotherdropdown.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Bindonedropdowntoanotherdropdown : 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);//It is use to Execute query
        //Datable me value aayage SqlDataAdapter se
        DataTable data = new DataTable();
        sda.Fill(data);
        CountryDropDownList.DataSource = data;
        CountryDropDownList.DataTextField = "country_name";
        CountryDropDownList.DataValueField = "country_id";
        CountryDropDownList.DataBind();
        //Select item show hoga dropdown list me

        ListItem selectItem = new ListItem("Select Country","Select Country");
        selectItem.Selected = true;
        //Phala option me select country show hoga[It can show in 0 index]
        CountryDropDownList.Items.Insert(0,selectItem);
    }
    void BindCityDDL(int country_id)//int is datatype and country_id is parameter[Parameter pass ho raha ha]
    {
        SqlConnection con = new SqlConnection(cs);
        string query = "select * from city where c_id = @country_id";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);//It is use to Execute query
        //sda se command select kar raha ha
        sda.SelectCommand.Parameters.AddWithValue("@country_id", country_id);
        //Datatable me value aayage SqlDataAdapter se

        DataTable data = new DataTable();
        sda.Fill(data);
        CityDropDownList.DataSource = data;
        CityDropDownList.DataTextField = "city_name";
        CityDropDownList.DataValueField = "city_id";
        CityDropDownList.DataBind();
        //Select item show hoga dropdown list me
        ListItem selectItem = new ListItem("Select City", "Select City");
        selectItem.Selected = true;
        //Phala option me select city show hoga[It can show in 0 index]
        CityDropDownList.Items.Insert(0, selectItem);
    }

    protected void CountryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            //Upper me country id int diya gaya ha usili liya Integer me convert karna ha
            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 Country Id is:" + CityDropDownList.SelectedValue.ToString()+"<br>");
    }
}//Auto Postback True karna pa selected index change ka event fire hoga

Post a Comment

0 Comments