Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在下拉列表中动态添加值_Asp.net_Drop Down Menu - Fatal编程技术网

Asp.net 在下拉列表中动态添加值

Asp.net 在下拉列表中动态添加值,asp.net,drop-down-menu,Asp.net,Drop Down Menu,我有一个州和城市的下拉列表。当用户从第一个下拉列表中选择任何状态时,城市将反映在另一个下拉列表中。我在城市下拉列表中提供了其他选项。以便用户可以为各自的添加添加自己的城市。默认情况下,文本框被隐藏,当用户从城市下拉列表中选择其他选项时,文本框可见。我想在下拉列表中插入文本框值。我试着用下面的代码和帮助按钮,但它不适合我。请看代码 protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e) { i

我有一个州和城市的下拉列表。当用户从第一个下拉列表中选择任何状态时,城市将反映在另一个下拉列表中。我在城市下拉列表中提供了其他选项。以便用户可以为各自的添加添加自己的城市。默认情况下,文本框被隐藏,当用户从城市下拉列表中选择其他选项时,文本框可见。我想在下拉列表中插入文本框值。我试着用下面的代码和帮助按钮,但它不适合我。请看代码

 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 into Career.Job Values('" + txtOtherCity.Text.Trim() + "') where 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
    }
    finally
    {
        conn.Close();
    }
}
另外,请参见下拉列表、文本框和按钮的html代码

  <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>
        &nbsp;&nbsp;&nbsp;
                    <asp:button id="btnAddDropDown" runat="server" width="63" text="Add" causesvalidation="false" />
    </td>
</tr>

地点/城市

请帮忙

我像这样添加了它,效果很好

  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));
    }
}

否。将额外的城市添加到数据库(最好使用存储过程,而不是通过执行sql字符串)-在存储过程中放入一些错误捕获代码,以便它返回一个值来指示成功或错误-如果成功,请重新绑定城市下拉列表,以便它显示新值。如何,我希望用户在运行时添加它。或者你说的方式可能吗?我自己做的。无论如何,非常感谢。