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 HussainPosted Aug 1, 2013, 12:05 AM
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
Khargesh RajputPosted Aug 1, 2013, 1:27 AM
thanks
Sanjeeb LenkaPosted Aug 1, 2013, 12:21 AM
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();
}
}