Choose Country state corresponding city list appears without refreshing the page

Satyapriyanayak
Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 1333
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Country_state_city._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label1" runat="server" Text="Choose Country" BackColor="#FFFF99"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlcountry" AutoPostBack ="true"
AppendDataBoundItems="true" runat="server" Height="20px" Width="156px"
onselectedindexchanged="ddlcountry_SelectedIndexChanged" Font-Bold="True"
ForeColor="#FF9900">
</asp:DropDownList>
</ContentTemplate>

<Triggers>
<asp:AsyncPostBackTrigger ControlID ="ddlcountry" />
</Triggers>
</asp:UpdatePanel>
<br />

<asp:UpdatePanel ID="statepanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label2" runat="server" Text="Choose State" BackColor="#FFFF66"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlstate" AutoPostBack ="true"
AppendDataBoundItems ="true" runat="server" Height="20px"
Width="155px" onselectedindexchanged="ddlstate_SelectedIndexChanged" Font-Bold="True"
ForeColor="#993300">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlstate" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="citypanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label3" runat="server" Text="Choose City" BackColor="#FFFF99"
ForeColor="Red" Width="100px"></asp:Label>
<asp:DropDownList ID="ddlcity" AutoPostBack ="true" AppendDataBoundItems ="true"
runat="server" Height="20px" Width="155px" Font-Bold="True" ForeColor="#CCCC00">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlcity" /></Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Country_state_city
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindcountry();
}
}

protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
bindstate();
}

protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
bindcity();
}


public void bindcountry()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CountryId,CountryName from Country";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Country");
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Select--");
ddlcountry.DataValueField = "CountryId";
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataSource = ds;
ddlcountry.DataMember = "Country";
ddlcountry.DataBind();
con.Close();
}
public void bindstate()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select StateId,StateName from State where Country_Id='" + ddlcountry.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "State");
ddlstate.Items.Clear();
ddlstate.Items.Add("--Select--");
ddlstate.DataValueField = "StateId";
ddlstate.DataTextField = "StateName";
ddlstate.DataSource = ds;
ddlstate.DataMember = "State";
ddlstate.DataBind();
con.Close();
}
public void bindcity()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CityId,CityName from City where State_Id ='" + ddlstate.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "City");
ddlcity.Items.Clear();
ddlcity.Items.Add("--Select--");
ddlcity.DataValueField = "CityId";
ddlcity.DataTextField = "CityName";
ddlcity.DataSource = ds;
ddlcity.DataMember = "City";
ddlcity.DataBind();
con.Close();
}
}
}

Comments or Responses

Login to post response