one for country,one for state and the last one for city..
when I select any country from the first dropdown the and then the second will be enabled and will show only that states of a country which i have selected in drop down list.
That is working properly but because of the Auto postback property is true so each time i select country then the page is refreshed then after i select state again the page is refreshed and for city also the page is refreshed.so the page is refreshed thrice.
that i don't want to do.so what should i do to avoid the thrice page refresh??
can I use Ajax cascading drop down list??
How??
Satyapriya NayakPosted Apr 1, 2013, 12:14 AM
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();
}
}
}
harshit wadheraPosted Sep 15, 2013, 4:20 PM
darsh antaniPosted Apr 1, 2013, 8:49 PM