C# 提交后清除所有字段

C# 提交后清除所有字段,c#,asp.net,C#,Asp.net,我在asp.net应用程序中有许多文本框,提交它们的值后,我想在再次加载时清除所有字段?您可以使用JavaScript form reset()方法或循环抛出所有文本框,并将文本属性设置为空字符串 foreach(var control in this.Controls){ TextBox tb = control as TextBox; if (tb != null) { tb.Text = string.Empty; } } 您需要在提交后编写并调用类似

我在asp.net应用程序中有许多文本框,提交它们的值后,我想在再次加载时清除所有字段?

您可以使用JavaScript form reset()方法或循环抛出所有文本框,并将文本属性设置为空字符串

foreach(var control in this.Controls){
   TextBox tb = control as TextBox;
   if (tb != null)
   {
      tb.Text = string.Empty;
   }
}

您需要在提交后编写并调用类似的
函数

   public static void EmptyTextBoxes(Control parent) 
   { 
      foreach (Control c in parent.Controls) { 
        if (c.GetType() == typeof(TextBox))
        { 
           ((TextBox)(c)).Text = string.Empty;            
        }  
      }
   }
参考文献


您可以在页面中执行此操作:

foreach (var tb in Controls.OfType<TextBox>())
{
  tb.Text = null;
}
公共静态控件[]层次结构(控件根)
{
列表=新列表();
list.Add(根目录);
if(root.HasControls())
{
foreach(root.Controls中的控件)
{
列表.添加范围(层级(控制));
}
}
return list.ToArray();
}
私有void cleartextbox()
{
控件[]所有控件=层次结构(第页);
foreach(所有控件中的控件)
{
TextBox TextBox=控件作为TextBox;
如果(文本框!=null)
{
textBox.Text=”“;
}
}
}

此代码将收集列表中的所有文本框,并将其textproperty设置为“”。

您也可以将该字段设置为不使用viewstate,这将意味着性能的提升,虽然很小,但仍然是一个提升。 此外,您还可以执行response.redirect到同一页面或server.transfer。

如果这两种解决方案不适合您,您可以使用textbox.text=string.empty和dropdownlist.clearselection。它们可能没有您想要的那么快,它们更优雅。

完全未经测试的解决方案:

您不能在Init或LoadViewState事件处理程序中使用ME.ViewState.Clear()吗

或者它可能是Page.Viewstate.Clear()或者甚至是Page.ClearChildViewState()


抱歉-没有在愤怒中尝试过…

这用于清除表单中的所有控件,如文本框、复选框、radioButton

您可以添加所需的不同类型

private void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Clear();
            }

            if (c.HasChildren)
            {
                ClearTextBoxes(c);
            }


            if (c is CheckBox)
            {

                ((CheckBox)c).Checked = false;
            }

            if (c is RadioButton)
            {
                ((RadioButton)c).Checked = false;
            }
        }
    }

创建一个名为
cleardata()
的函数:


并在需要时调用此函数。

这是一个老问题,但我只想添加以下内容,如果控件位于组框中,您可以这样做,然后:

        foreach (Control c in this.form.Controls)
        {
            //Tests each control to see if it is a GroupBox
            if (c is GroupBox)
            {
                clearRadioButtons(c.Controls);
                clearListBox(c.Controls);
                resetDateTime(c.Controls);
                clearTextBoxes(c.Controls);
                clearComboBoxes(c.Controls);
            }
        }    
    public static void clearTextBoxes(Control.ControlCollection controls)
    {
        //Loops through all controls on form
        foreach (Control c in controls)
        {
            //Tests each control to see if it is a textbox
            if (c is TextBox)
            {
                //Converts to useable format and clears textboxes
                var text = (TextBox)c;
                text.Clear();
            }
        }
    }

只要写这一行,所有字段都会重置。this.form1.ID=“newid”;可能有用,但引用表单ID的情况除外(尽管出于明显的原因,在ASP.NET中情况往往不是这样)
private void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Clear();
            }

            if (c.HasChildren)
            {
                ClearTextBoxes(c);
            }


            if (c is CheckBox)
            {

                ((CheckBox)c).Checked = false;
            }

            if (c is RadioButton)
            {
                ((RadioButton)c).Checked = false;
            }
        }
    }
void cleardata()
{
    textbox1.Clear();
    textbox2.Clear();
    textbox3.Clear();
    textbox4.Clear();
}
        foreach (Control c in this.form.Controls)
        {
            //Tests each control to see if it is a GroupBox
            if (c is GroupBox)
            {
                clearRadioButtons(c.Controls);
                clearListBox(c.Controls);
                resetDateTime(c.Controls);
                clearTextBoxes(c.Controls);
                clearComboBoxes(c.Controls);
            }
        }    
    public static void clearTextBoxes(Control.ControlCollection controls)
    {
        //Loops through all controls on form
        foreach (Control c in controls)
        {
            //Tests each control to see if it is a textbox
            if (c is TextBox)
            {
                //Converts to useable format and clears textboxes
                var text = (TextBox)c;
                text.Clear();
            }
        }
    }