I’ve got two dropdowns
- category
- subcategory
If a category is selected, then second dropdown should automatically update, so I’ve written this code below for 1st dropdown:
public void bindcategory()
{
DataTable dt = new BALCate().GetCate();
DropDownList dropdownlist = new DropDownList();
foreach (DataRow dr in dt.Rows)
{
ListItem listitem = new ListItem();
listitem.Text = dr["cate_name"].ToString();
dropdownlist.Items.Add(listitem);
}
cate_search.Controls.Add(dropdownlist);
}
but writing 2nd dropdown code getting some error and also confused that how could to get the 1st dropdown select value, because 1st dropdown declared inside of bindcategory() block, thats why it couldn’t be accessed in other blocks. So what should I do for that?
public void bindsubcategory()
{
//error (selected cate_id from 1st dropdown cant accessed due to scop problem)
DataTable dt = new BALCate().GetSubCate( //some cate_id );
// what should the code here?
}
Is there any other way to do this?
You are missing few things. See the sample code below