简体   繁体   中英

How to prevent re-type info when SelectedIndexChanged Asp.NET C#

I am facing an issue with my DropDrown SelectedIndexChanged Method, I've coded an Asp.NET DropDown to populate a textbox according to the selected option, however the page needs to pass through several processes before get to the DropDown button, so the SelectedIndexChanged works fine, it populates my Textbox, however after populating the textbox the page runs a PostBack and refreshes the entire site so, the radionbuttons and all the selection I've done are removed, so I need to fill them up all over again and check again the radionbuttons, fill up other textboxes and start my form from scratch, and besides that the pages loads as if I were starting all again.

HTML

<div class="form-group">
   <label for="name" class="text-muted"><small><strong>Código del cliente:</strong></small></label>
       <asp:DropDownList AutoPostBack="true" ID="ddClientCode" OnSelectedIndexChanged="ddClientCode_SelectedIndexChanged" runat="server" CssClass="form-control input-sm btn-box-tool"></asp:DropDownList>
    </div>
       <div class="form-group">
       <label for="name" class="text-muted"><small><strong>Persona jurídica:</strong></small></label>
       <asp:TextBox ID="TxtLegalPerson1" CssClass="form-control" runat="server" placeholder="Persona jurídica" />
       </div>

The code behind

#region Populate Textboxes Based On Selected Index Client
    protected void ddClientCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        string CLIENTE = ddClientCode.SelectedValue.ToString();
        TxtLegalPerson1.Text = BindCedulaJuridica(CLIENTE);
    }
    #endregion

    #region Bind [Cedula Juridica] based on ddClientCode
    public string BindCedulaJuridica(string x)
    {
        string returnValue = string.Empty;
        try
        {
            string constr = ConfigurationManager.ConnectionStrings["wiz"].ConnectionString;
            using (IDbConnection connection = new SqlConnection(constr))
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT Column FROM TABLE AS A where Client= '" + x + "'";
                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            returnValue = reader["Column"].ToString();
                        }

                        if (connection.State == ConnectionState.Open)
                        {
                            connection.Close();
                        }
                        return returnValue;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string err = ex.ToString();
        }
        return returnValue;
    }
    #endregion

Page Load

protected void Page_Load(object sender, EventArgs e)
    {
        #region User Validation
        string Group = (string)(Session["Group"]);
        switch (Group)
        {
            case "a":
                //Response.Redirect("/Dashboard");//This is temporal
                break;
            case "b":
                //Response.Redirect("/Dashboard");
                break;
            case "c":
                Response.Write("<script>alert('¡Lo sentimos, usted no cuenta con privilegios para crear reportes!');</script>");
                Response.Redirect("/Dashboard");
                break;
            case "d"://Check this name with the database
                //Response.Redirect("/Dashboard");
                break;
            default:
                break;
        }
        #endregion

        if (!IsPostBack)
        {
            this.FillTypes();
            this.FillStatationsDropDown();
            this.FillVehiculeCode();
            this.FillddOutAgency();
            this.FillddInAgency();
            this.FillCustomers();
        }
    }

Is there any way to populate my textbox without refreshing the page and witout loosing all the info that I've filled up?

Thanks in advance.

1 . The first thing that causes this problem is Page_Load event that loads after the control events like button click or dropdown index change. It could be solved by using if (!IsPostBack) that you used.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    //if you have some default value settings for the dropdown, write here.
    }
}

2 . When your page posts back you have a new page. The only thing that makes your controls to have their old values, is ViewState . ViewState is an string that contains some data to set your page. All of the controls inherit the viewState enable/disable from the page. The ViewState of each control could be set separately.

I Think : The ViewState contains the Selected Value of the dropdown, So if the dropdown has repeated values in different indexes, after the page load it would be unknown the real selected index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM