简体   繁体   English

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

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

HTML 的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 . 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. 导致此问题的第一件事是Page_Load事件,该事件在控件事件(如按钮单击或下拉索引更改) 加载。 It could be solved by using if (!IsPostBack) that you used. 可以通过使用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 . 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 ViewState is an string that contains some data to set your page. ViewState是一个字符串,其中包含一些用于设置页面的数据。 All of the controls inherit the viewState enable/disable from the page. 所有控件都继承页面的viewState启用/禁用。 The ViewState of each control could be set separately. 每个控件的ViewState可以分别设置。

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

暂无
暂无

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

相关问题 ASP.NET/C# SelectedIndexChanged无法启动 - ASP.NET/C# SelectedIndexChanged not firing on asp.net c#在从服务器端代码更改索引时阻止触发selectedindexchanged事件 - asp.net c# prevent firing selectedindexchanged event while changing index from serverside code SelectedIndexChanged嵌套GridView asp.net C# - SelectedIndexChanged Nested GridView asp.net C# ASP.NET / C#:DropDownList SelectedIndexChanged事件未触发 - ASP.NET / C#: DropDownList SelectedIndexChanged event not firing ASP.NET MVC C#-DropDownList SelectedIndexChanged不触发 - Asp.net MVC C# - DropDownList SelectedIndexChanged not firing ASP.NET / C#:DropDownList在服务器控件中没有触发的SelectedIndexChanged - ASP.NET / C#: DropDownList SelectedIndexChanged in server control not firing 在 asp.net C# 中第一次执行该事件后,如何让 RadioButtonList_SelectedIndexChanged 事件处理程序执行? - How do I get the RadioButtonList_SelectedIndexChanged event handler to execute after the first execution of that event in asp.net C#? 如何防止Warez上载。 (asp.net,C#) - How to prevent the uploading of Warez. (asp.net, C#) 如何防止ASP.NET的C#中的文本转义? - How to prevent escaping of text in C# for ASP.NET? 当RadioButtonList选择asp.net C#时,SelectedIndexChanged无法正常工作? autopostback还设置为true吗? - SelectedIndexChanged not working while RadioButtonList selected asp.net c#? autopostback also set to true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM