繁体   English   中英

在SelectedIndexChanged Asp.NET C#中如何防止重新键入信息

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

我的DropDrown SelectedIndexChanged方法遇到问题,我已经编码了一个Asp.NET DropDown来根据选择的选项填充文本框,但是该页面需要经过多个过程才能到达DropDown按钮,因此SelectedIndexChanged可以正常工作很好,它会填充我的文本框,但是在填充文本框后,页面会运行“回发”并刷新整个站点,因此,单选按钮和我所做的所有选择都将被删除,因此我需要再次将其填满并再次检查单选按钮,填充其他文本框并从头开始创建表单,此外,页面的加载就像我重新开始一样。

的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>

背后的代码

#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

页面加载

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

有什么方法可以填充我的文本框,而无需刷新页面并且不丢失我填写的所有信息?

提前致谢。

1 导致此问题的第一件事是Page_Load事件,该事件在控件事件(如按钮单击或下拉索引更改) 加载。 可以通过使用if (!IsPostBack)来解决。

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

2 页面发回时,您将创建一个新页面。 使控件具有旧值的唯一方法是ViewState ViewState是一个字符串,其中包含一些用于设置页面的数据。 所有控件都继承页面的viewState启用/禁用。 每个控件的ViewState可以分别设置。

我认为 :ViewState包含下拉列表的“选定值”,因此,如果下拉列表在不同的索引中具有重复的值,则在页面加载后,真正的选定索引将是未知的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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