asp.net - When selecting value from dropdown list, it is resetting all other dropdown values -
when selecting value country drop-down list resetting value of other drop-down list boxes , selected country getting reset.
the country drop-down list, state drop-down list , district drop-down list dependent.
private void binddropdownlist(dropdownlist ddl, string query, string text, string value, string defaulttext) { string constring = configurationmanager.connectionstrings["constring"].connectionstring; sqlcommand cmd = new sqlcommand(query); using (sqlconnection con = new sqlconnection(constring)) { using (sqldataadapter sda = new sqldataadapter()) { cmd.connection = con; con.open(); ddl.datasource = cmd.executereader(); ddl.datatextfield = text; ddl.datavaluefield = value; ddl.databind(); con.close(); } } ddl.items.insert(0, new listitem(defaulttext, "0")); } protected void gcountry2_selectedindexchanged(object sender, eventargs e) { gstate2.enabled = false; gdistrict2.enabled = false; gstate2.items.clear(); gdistrict2.items.clear(); //gstate2.items.insert(0, new listitem("select state", "0")); //gdistrict2.items.insert(0, new listitem("select city", "0")); int countryid = int.parse(gcountry2.selecteditem.value); if (countryid > 0) { string query = string.format("select stateid, statename states countryid = {0}", countryid); binddropdownlist(gstate2, query, "statename", "stateid", "select state"); gstate2.enabled = true; page.setfocus(f2.clientid); } } protected void gstate2_selectedindexchanged1(object sender, eventargs e) { gdistrict2.enabled = false; gdistrict2.items.clear(); //gdistrict2.items.insert(0, new listitem("select city", "0")); int stateid = int.parse(gstate2.selecteditem.value); if (stateid > 0) { string query = string.format("select cityid, cityname cities stateid = {0}", stateid); binddropdownlist(gdistrict2, query, "cityname", "cityid", "select city"); gdistrict2.enabled = true; page.setfocus(f2.clientid); }
there easier way want. bind dropdowns declaratively. this.
<%-- .aspx file. --%> <%-- populate country ddl --%> <asp:dropdownlist id="ddcountry" runat="server" datatextfield="countryname" datavaluefield="countryid" appenddatabounditems="true" datasourceid="sqlcountry" autopostback="true"> <asp:listitem value="0" text="- select country -"></asp:listitem> </asp:dropdownlist> <asp:sqldatasource id="sqlcountry" runat="server" connectionstring="<%$ connectionstrings:connstring %>" selectcommand="select countryid,countryname dbo.countries"> </asp:sqldatasource> <%-- populate state ddl --%> <asp:dropdownlist id="ddstate" runat="server" datatextfield="statename" datavaluefield="stateid" datasourceid="sqlstate" autopostback="true" ondatabound="ddstate_databound"> </asp:dropdownlist> <asp:sqldatasource id="sqlstate" runat="server" connectionstring="<%$ connectionstrings:connstring %>" selectcommand="select stateid, statename dbo.states countryid=@countryid"> <selectparameters> <%-- bind parameter parent control --%> <asp:controlparameter defaultvalue="0" controlid="ddcountry" propertyname="selectedvalue" name="countryid" type="int32" /> </selectparameters> </asp:sqldatasource> <%-- populate district ddl --%> <asp:dropdownlist id="dddistrict" runat="server" datatextfield="cityname" datavaluefield="cityid" datasourceid="sqldistrict" autopostback="true" ondatabound="dddistrict_databound"> </asp:dropdownlist> <asp:sqldatasource id="sqldistrict" runat="server" connectionstring="<%$ connectionstrings:connstring %>" selectcommand="select cityid, cityname dbo.cities stateid=@stateid"> <selectparameters> <asp:controlparameter defaultvalue="0" controlid="ddstate" propertyname="selectedvalue" name="stateid" type="int32" /> </selectparameters> </asp:sqldatasource>
you need code behind add "-- select --" items 2 last dropdowns.
// .aspx.cs protected void ddstate_databound(object sender, eventargs e) { ddstate.items.insert(0, new listitem("--select state--", "0")); } protected void dddistrict_databound(object sender, eventargs e) { dddistrict.items.insert(0, new listitem("--select city--", "0")); }
Comments
Post a Comment