单击按钮时,下拉列表会丢失其值

时间:2014-08-20 14:17:38

标签: c# asp.net drop-down-menu

我有两个下拉菜单。第一个是国家,第二个是城市。当我选择州时,其下一个城市将在下一个下拉列表中填充。我有一个选项可以在按钮单击时通过文本框添加更多城市,同时从城市下拉列表中选择其他选项。

问题在于,无论何时我从文本框添加城市,它都会插入到相应的状态,并且下拉列表也会丢失其值。我试过调试但找不到确切的问题。

请参阅代码供您参考:

protected void btnAddDropDown_Click1(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_CityforLocation";
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
        cmd.Parameters.Add("@CountryName", SqlDbType.VarChar).Value = "India";
        cmd.Parameters.Add("@CityName", SqlDbType.VarChar).Value = txtOtherCity.Text.Trim();
        cmd.Parameters.Add("@StateName", SqlDbType.VarChar).Value = ddllocation1.SelectedItem.ToString();
        cmd.Connection = con;
        try
        {
            cmd.ExecuteNonQuery();
            BindContrydropdown();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);//You Can Haave Messagebox here
        }
        finally
        {
            con.Close();
        }
    }
}

protected void BindContrydropdown()
{
    //conenction path for database
    //string connection = WebConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Id,CityName From Career.Location", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddllocation1.DataSource = ds;
        ddllocation1.DataTextField = "CityName";
        ddllocation1.DataValueField = "Id";
        ddllocation1.DataBind();
        ddllocation1.Items.Insert(0, new ListItem("--Select--", "0"));
        ddllocation1.Items.Insert(1, new ListItem("--OTHER--", "0"));
        con.Close();
    }
}

另外,请参阅相同的HTML: -

 <tr>
        <td class="td">Location/State</td>
        <td>
            <asp:DropDownList CssClass="txtfld-popup" ID="ddllocation1" OnSelectedIndexChanged="ddllocation1_SelectedIndexChanged" runat="server" AutoPostBack="true"></asp:DropDownList>
            <asp:RequiredFieldValidator CssClass="error_msg" ID="RequiredFieldValidator1" ControlToValidate="ddllocation1" runat="server" ErrorMessage="Please enter location" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <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" OnClick="btnAddDropDown_Click1" />
        </td>
    </tr>

1 个答案:

答案 0 :(得分:0)

试试这个

try
{

    if(!IsPostBack)
    {
        cmd.ExecuteNonQuery();
        BindContrydropdown();
    }

}