代码之家  ›  专栏  ›  技术社区  ›  Slauma

如何从代码隐藏中清除所有表单字段?

  •  5
  • Slauma  · 技术社区  · 15 年前

    <input type="reset" ... />

    有没有类似的简单方法从代码隐藏中重置aspx页面的所有表单字段?或者是否有必要用 TextBox1.Text=string.Empty TextBox2.Text=string.Empty 等等?

    提前谢谢!

    上下文是一个简单的联系/“给我们一个消息”页8asp:TextBoxes on 页面(用户在其中输入姓名、地址、电话、电子邮件、消息等)。然后他单击submit,codebhind中的Onclick消息处理程序向某个管理员发送一封电子邮件,用户填写的所有表单字段都应该清空,他在标签中收到一个通知(“messagesent blablabla…”)。我希望清除表单字段,以避免用户再次单击submit并再次发送相同的消息。

    5 回复  |  直到 15 年前
        1
  •  11
  •   Thomas    15 年前

    您只需要为每种类型的控件编写一个fork,除非其中一个控件有需要执行的特殊操作来重置它。

    foreach( var control in this.Controls )
    {
        var textbox = control as TextBox;
        if (textbox != null)
            textbox.Text = string.Empty;
    
        var dropDownList = control as DropDownList;
        if (dropDownList != null)
            dropDownList.SelectedIndex = 0;
        ...
    }
    

    你问过如何清除控制装置,即使是被掩埋的控制装置。为此,应创建如下递归例程:

    private void ClearControl( Control control )
    {
        var textbox = control as TextBox;
        if (textbox != null)
            textbox.Text = string.Empty;
    
        var dropDownList = control as DropDownList;
        if (dropDownList != null)
            dropDownList.SelectedIndex = 0;
        ...
    
        foreach( Control childControl in control.Controls )
        {
            ClearControl( childControl );
        }
    }
    

    因此,您可以通过传递页面来调用它:

    ClearControls( this );
    
        2
  •  7
  •   Kamal Pratap    9 年前

    有关详细信息,请参阅此链接

    http://www.freshcodehub.com/Article/3/clear-all-fields-like-textbox-dropdownlist-checkbox-radiobutton-label-after-form-submission-in-aspnet-c

    public void ClearControls(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if ((c.GetType() == typeof(TextBox)))  //Clear TextBox
            {
                ((TextBox)(c)).Text = "";
            }
            if ((c.GetType() == typeof(DropDownList)))  //Clear DropDownList
            {
                ((DropDownList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(CheckBox)))  //Clear CheckBox
            {
                ((CheckBox)(c)).Checked = false;
            }
            if ((c.GetType() == typeof(CheckBoxList)))  //Clear CheckBoxList
            {
                ((CheckBoxList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(RadioButton)))  //Clear RadioButton
            {
                ((RadioButton)(c)).Checked = false;
            }
            if ((c.GetType() == typeof(RadioButtonList)))  //Clear RadioButtonList
            {
                ((RadioButtonList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(HiddenField)))  //Clear HiddenField
            {
                ((HiddenField)(c)).Value = "";
            }
            if ((c.GetType() == typeof(Label)))  //Clear Label
            {
                ((Label)(c)).Text = "";
            }
            if (c.HasControls())
            {
                ClearControls(c);
            }
        }
    }
    
        3
  •  4
  •   Shaminder Singh    5 年前

    使用手动方法 String.Empty 对于每一个文本框或任何其他字段都会很麻烦,也可以使用 Response.Redirect();

        Public void reset(Control control)
        {
          foreach (Control x in control.Controls)
          {
            if (x is TextBox)
            {
                (x as TextBox).Text = String.Empty;
            }
            else if (x is DropDownList)
            {
                (x as DropDownList).SelectedIndex = 0;
            } 
            .
            .
            reset(x);
          }
        }
    
    
              
    

    将此代码用作 reset(this); 在您的页面中,您可以在任何地方重置或清除值。在比赛结束时 if

    `控件`object x。
        4
  •  2
  •   Jack Ishu    13 年前

    使用 form.Controls.Clear() 不是很好的方法,因为它会清除整个窗体,甚至会丢失窗体上的所有按钮。

    相反,如果您只想清除所有表单字段(如文本字段和单选按钮),我建议您尝试以下操作: 如果你有一个重置按钮1,那么点击调用一个函数 reset();

    在复位功能中:

       protected void resetButton_Click(object sender, EventArgs e)
       {
          TextBox1.Text=""; //set equal to empty string to all fields
       }
    

    或通过终止上一页重定向到同一页

        protected void resetButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Test2.aspx", true);
        } 
    
        5
  •  1
  •   Mikael Svenson    15 年前

    EnableViewState=false )您希望在提交后显示为空的控件。

    推荐文章