提交后清除所有字段

She*_*ery 6 c# asp.net

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

Asa*_*sad 14

你需要编写,并呼吁类似的functionsubmit

   public static void EmptyTextBoxes(Control parent) 
   { 
      foreach (Control c in parent.Controls) { 
        if (c.GetType() == typeof(TextBox))
        { 
           ((TextBox)(c)).Text = string.Empty;            
        }  
      }
   }
Run Code Online (Sandbox Code Playgroud)

参考

http://www.tek-tips.com/faqs.cfm?fid=6470


Cha*_*ori 5

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

你可以添加你想要的不同类型..

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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)