Search code examples
sqlasp.netautopostback

ASP.Net DropDownList Retains Value From Database - AutoPostBack


I have been stuck in this problem for along time, searched online but no solution yet solved my problem. I have a dropdownlist where it retrieved the plan names from a table in my databse based on the zip code that is entered in a text field. The problem I am running into is: when I enter a zip code, the plan names populate with no problem in the dropdownlist, then I select one of the plans, when I go further and try to enter other info in other fields which have autppostback = true, the dropdownlist does not retain its value and always select the first value, I want the dropdownlist to retain the selected value after the postback, here is my code:

step1.aspx

<p>
    <asp:TextBox ID="txt_zip_code" runat="server" AutoPostBack="true"></asp:TextBox>
</p>
<p>    
    <asp:DropDownList ID="dd_dental_plan" runat="server" EnableViewState="true">
</asp:DropDownList>

</p>

step1.cs

 protected void Page_Load(object sender, EventArgs e)
    {

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString))
        {
            using (SqlCommand sqlCmd = new SqlCommand())
            {
                sqlCmd.CommandText = "SELECT * FROM [dbo].[mfd_plans] WHERE zip_code = @zipCode";
                sqlCmd.Parameters.AddWithValue("@zipCode", txt_zip_code.Text);

                sqlCmd.Connection = conn;
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dd_dental_plan.DataSource = dt;
                dd_dental_plan.DataValueField = "plan_id";
                dd_dental_plan.DataTextField = "plan_name";
                dd_dental_plan.DataBind();
                conn.Close();
                dd_dental_plan.Items.Insert(0, new ListItem("Please select plan", "0"));
            }
        }

Solution

  • you should use update panel on your aspx page for preventing whole page load when autopostback occurs from another fields. just put an asp:scriptmanager control on your page. After the put your zip code and dropdownlist into update panel and put other controls in separate update panels by which post back occurs. your code will look like this.

      <asp:ScriptManager runat="server">
      </asp:ScriptManager>
      <div>
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <p>
                <asp:TextBox ID="txt_zip_code" runat="server" AutoPostBack="true"></asp:TextBox>
            </p>
            <p>    
                 <asp:DropDownList ID="dd_dental_plan" runat="server" EnableViewState="true">
                </asp:DropDownList>
    
            </p>
        </ContentTemplate>
        </asp:UpdatePanel>
    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
                another controls which caused postback event
        </ContentTemplate>
        </asp:UpdatePanel>
        another controls which don't cause postback.
    </div>
    

    Hope this will works.enter code here

    Thanks