asp.net - Dynamically Adding values in dropdown -
i have dropdowns of state , city. when user selects state first dropdown city reflects in other dropdown. have given other option in city dropdown. user can add own city respective add. here happening textbox been hided default, visible when user select other option city dropdown. want textbox value inserted in dropdown. tried below code button not working me. please see code.
protected void ddllocation_selectedindexchanged(object sender, eventargs e) { if (ddllocation.selecteditem.text == "other") { txtothercity.visible = true; } else { txtothercity.visible = false; } } protected void btnadddropdown_click(object sender, eventargs e) { conn = new sqlconnection(); try { conn.open(); if (txtothercity.text != "") { ddllocation1.items.insert(0, txtothercity.text); txtothercity.text = ""; } string commandtext = "insert career.job values('" + txtothercity.text.trim() + "') location='" + ddllocation1.selecteditem.text + "'"; sqlcommand cmd = new sqlcommand(commandtext); cmd.commandtype = commandtype.text; cmd.executenonquery(); conn.close(); } catch (exception ex) { response.write(ex.message);//you can haave messagebox here } { conn.close(); } }
also, see html code dropdown, textbox , button
<tr> <td class="td">location/city</td> <td> <asp:dropdownlist cssclass="txtfld-popup" id="ddllocation" runat="server" autopostback="true" onselectedindexchanged="ddllocation_selectedindexchanged"></asp:dropdownlist> <asp:requiredfieldvalidator cssclass="error_msg" id="reqlocation" controltovalidate="ddllocation" runat="server" errormessage="please enter location" initialvalue="--select--" setfocusonerror="true"></asp:requiredfieldvalidator> </td> </tr> <tr> <td> <asp:textbox id="txtothercity" runat="server" visible="false" cssclass="txtfld-popup"></asp:textbox> <asp:button id="btnadddropdown" runat="server" width="63" text="add" causesvalidation="false" /> </td> </tr>
please help.
i added , worked.
protected void ddllocation_selectedindexchanged(object sender, eventargs e) { if (ddllocation.selecteditem.text == "other") { txtothercity.visible = true; } else { txtothercity.visible = false; } } protected void btnadddropdown_click1(object sender, eventargs e) { string city = txtothercity.text.trim(); if (!string.isnullorempty(city)) { ddllocation.items.add(new listitem(city, city)); } }
Comments
Post a Comment