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

时间:2010-04-10 14:52:49

标签: asp.net webforms

HTML有一个输入按钮类型,可以在一个步骤中将表单中的所有字段重置为初始状态:<input type="reset" ... />

是否有一种类似的简单方法可以从代码隐藏中重置aspx页面的所有表单字段?或者是否需要使用TextBox1.Text=string.EmptyTextBox2.Text=string.Empty等逐个重置所有控件?

提前致谢!

更新

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

5 个答案:

答案 0 :(得分:11)

您只需为每种类型的控件编写一个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;
    ...
}

ADDITION 您询问了如何清除控件甚至是埋藏的控件。要做到这一点,你应该创建一个递归例程,如下所示:

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

答案 1 :(得分:7)

请参阅此链接以获取更多信息

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

答案 2 :(得分:3)

对每个文本框或任何其他字段使用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;
        }
        if (x is DropDownList)
        {
            (x as DropDownList).SelectedIndex = 0;
        } 
        .
        .
        reset(x);
      }
    }

在您想要重置或清除值的页面中,将此代码用作reset(this);。在if条件结束时,不要忘记使用相同的递归方式使用该函数 Control对象x。

答案 3 :(得分:2)

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

相反,如果您只想清除所有表单字段,如文本字段和单选按钮,我建议您尝试以下操作: 如果你有一个重置按钮“Button1”然后点击调用一个函数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);
    } 

答案 4 :(得分:1)

对于您的方案,在我看来,清除字段的最简单方法是在提交后关闭要显示为空白的控件的ViewState(EnableViewState=false)。

或者对于整个页面,除非你需要一些状态。

相关问题