I have a dropdownlist for states.
and on selected index change i want the cities of the selected state in listbox.
I have bind states in dropdownlist, but on selected index chnge i got only default value.
public void bindstate()
{
DataSet ds = new DataSet();
ds = objdal.FillDataSet(CommandType.Text, "select * from state where active=1");
//ddlstate.Items.Clear();
//ddlstate.Items.Add("Select State");
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "StateId";
ddlstate.DataSource = ds.Tables[0];
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("-- Select State --", "0"));
and
DataSet ds = new DataSet();
ds = objdal.FillDataSet(CommandType.Text, "select City_Name from City where State_id='"+ddlstate.SelectedValue+"'");
listcity.DataSource = ds.Tables["City"];
listcity.DataTextField = "City_name";
listcity.DataValueField = "CityID";
listcity.DataBind();
listcity.Items.Insert(0,new ListItem("select city","0"));
obkdal is object of DAL
Loading


Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 8:05 PM EDT
Try like this
DataSet ds = new DataSet();
ds = objdal.FillDataSet(CommandType.Text, "select City_Name from City where State_id='"+ddlstate.SelectedValue+"'");
listcity.DataSource = ds;
listcity.DataTextField = "City_name";
listcity.DataValueField = "CityID";
listcity.DataBind();
listcity.Items.Insert(0,new ListItem("select city","0"));
call this only inside the selected index changed of dllstate
Regards,
Iftikar
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 31, 2013, 8:21 PM EDT
your code looks correct. As you are getting always default value on the selectedindex changed event. so i guess you forgot to write the bIndState method in page_Load within the Ispostback. write it within it and check .
ex:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindstate();
}
}
Khargesh RajputRank #616 · 3.9K reputationPosted Jul 31, 2013, 9:27 PM EDT
thanks