Showing posts with label state and city in ASP. NET. Show all posts
Showing posts with label state and city in ASP. NET. Show all posts

Friday, 10 October 2014

DropDownList with country, state and city in ASP. NET,Country State City Cascading Dropdownlist in Asp.net

Hai friend today i am going to explain about DropDownList with country, state and city in ASP. NET
when user  selected country from on countries list  based on country all related states should be  bind to the dropdown & when the user select state from the state's list the user must get all region from region list .here we use a property as Auto Postback="True" for every Dropdown






<body style="background-image: url('colorful_background-wide.jpg'); height: 247px;">
    <form id="form1" runat="server">
    <div align="center">
   
    <table>
    <tr>
    <td>Select Country</td>
    <td>:</td>
  
    <td>      
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
             >
       
        <asp:ListItem>Select</asp:ListItem>
  
        </asp:DropDownList>

 </td>   </tr>
   <tr>
    <td>Select State</td>
    <td>:</td>
  
    <td>        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
        <asp:ListItem>Select</asp:ListItem>
        </asp:DropDownList>
 </td>   </tr>
   <tr>
    <td>Select region</td>
    <td>:</td>
   
    <td>        <asp:DropDownList ID="DropDownList3" runat="server">
        <asp:ListItem>Select</asp:ListItem>
        </asp:DropDownList>
 </td>   </tr>
    </table>
   
    </div>
    </form>
</body>


Code of Aspx.cs page 


Note:here i used  BindContrydropdown() in Page_Load  because !ispostback check for firsttime and excetue the BindContrydropdown() based on ispostback from next time it skip the page_load 

 if (!IsPostBack)
            {
                BindContrydropdown();
            }


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;


namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        public string connection = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindContrydropdown();
            }
        }


        protected void BindContrydropdown()
        {
            SqlConnection con = new SqlConnection(connection);

            SqlCommand cmd = new SqlCommand("select * from country", con);
          
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            con.Open();
            DataSet ds = new DataSet();

            da.Fill(ds);

            con.Close();

            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "countryname";
            DropDownList1.DataValueField = "countryid";
            DropDownList1.DataBind();

        }

      

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int countryid = Convert.ToInt32(DropDownList1.SelectedValue);
            SqlConnection con = new SqlConnection(connection);
            con.Open();

            SqlCommand cmd = new SqlCommand("select * from state where countryid=" + countryid, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            DropDownList2.DataSource = ds;
            DropDownList2.DataTextField = "StateName";
            DropDownList2.DataValueField = "StateID";
            DropDownList2.DataBind();

        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int stateid = Convert.ToInt32(DropDownList2.SelectedValue);
                 SqlConnection con = new SqlConnection(connection);
con.Open();
            SqlCommand cmd = new SqlCommand("select * from region where stateid=" + stateid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
            da.Fill(ds);
con.Close();

            DropDownList3.DataSource = ds;
            DropDownList3.DataTextField = "regionname";
DropDownList3.DataValueField = "regionid";
DropDownList3.DataBind();

        }

     

       







      
       
    }
}