c# - Populating dropdown list after selecting a value from another dropdown list -
i want able populate dropdown list(ddlexercise) after selecting value dropdown list(ddltype) @ moment getting values ddltype dropdown sql query populating dropdown list when select something, 2nd dropdown list remains empty. have 3 values in ddltype dropdown(gym,core,cardio) , once select 1 of these, using dropdownlist.selectedvalue retrieve corresponding results. here code:
protected void page_load(object sender, eventargs e) { sqlconnection con = new sqlconnection(@"con string"); if (!ispostback) { bindexercisetype(); } } public void bindexercisetype() { con.open(); sqlcommand cmd = new sqlcommand("select exercisetype exercisetype", con); sqldatareader dr = cmd.executereader(); ddltype.datasource = dr; ddltype.items.clear(); ddltype.items.add("--please select country--"); ddltype.datatextfield = "exercisetype"; ddltype.datavaluefield = "exercisetype"; ddltype.databind(); con.close(); } public void bindexercise() { con.open(); sqlcommand cmd = new sqlcommand("select exercisename exercisedisplay typename='" + ddltype.selectedvalue + "'", con); sqldatareader dr = cmd.executereader(); ddlexercise.datasource = dr; ddlexercise.items.clear(); ddlexercise.items.add("--please select country--"); ddlexercise.datatextfield = "exercisename"; ddlexercise.datavaluefield = "exercisename"; ddlexercise.databind(); con.close(); } protected void ddlexercise_selectedindexchanged(object sender, eventargs e) { bindexercise(); }
any ideas why may wrong? have autopostback propert set true on both drop down lists. appreciated!
the problem event, event other drop down, event should be:
protected void ddltype_selectedindexchanged(object sender, eventargs e) { bindexercise(); }
a contribution code, if populate dropdown datasource, items add not appear if before bind (). must add following, stay latest:
ddltype.items.insert(0, "--please select country--");
i hope have been helpful, , luck question. (sorry form english)
Comments
Post a Comment