Clear all fields after sending

I have a lot of text field in an asp.net application, and after sending their values ​​I want to clear all the fields when it loads again?

+6
source share
9 answers

you need to write and call a similar function after submit

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

link

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

+14
source share

And this is to clear all controls in the form of a text field, checkbox, radioButton

You can add various types that you want.

 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; } } } 
+4
source share

You can use the JavaScript reset () method or a loop by throwing all the text fields and setting the Text property to an empty string.

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

You can do this on the page:

 foreach (var tb in Controls.OfType<TextBox>()) { tb.Text = null; } 

If you need to clear the entire hierarchy, call it once from the page: ClearTextBoxes(this);

And here is the function:

 private void ClearTextBoxes(Control c) { foreach (var tb in c.Controls.OfType<TextBox>()) { tb.Text = null; } foreach (var control in c.Controls) { ClearTextBoxes(control); //Recurse down to children } } 
0
source share
 public static Control[] FlattenHierachy(Control root) { List<Control> list = new List<Control>(); list.Add(root); if (root.HasControls()) { foreach (Control control in root.Controls) { list.AddRange(FlattenHierachy(control)); } } return list.ToArray(); } private void ClearTextBoxes() { Control[] allControls = FlattenHierachy(Page); foreach (Control control in allControls) { TextBox textBox = control as TextBox; if (textBox != null) { textBox.Text = ""; } } } 

This code will collect all the text fields in the list and set their textproperty to "".

0
source share

Well, you can also set the field to not use the viewstate, which would mean a performance increase, but a slight increase, but nonetheless. Alternatively, you can do response.redirect on the same page or server.transfer.
If these two solutions do not suit you, you can use something like textbox.text = string.empty and dropdowlist.clearselection. They may not be as fast as you might wish for them to be much more elegant.

0
source share

TOTALLY UNCERTAINT DECISION:

Can't you use ME.ViewState.Clear () in Init or LoadViewState event handlers?

Or it could be a .Viewstate.Clear () page or even Page.ClearChildViewState () ...

Sorry - did not try in anger ....

0
source share

This is an old question, but I just wanted to add the following: if the controls are in a group field, you can do it like this:

  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(); } } } 
0
source share

Create a function called cleardata() :

 void cleardata() { textbox1.Clear(); textbox2.Clear(); textbox3.Clear(); textbox4.Clear(); } 

And call this function whenever you need it.

-one
source share

All Articles