7

I have many text box in an asp.net application, and after submitting their values i want to clear all fields when it loads again?

Bobby
  • 11,167
  • 5
  • 43
  • 68
Sheery
  • 1,113
  • 5
  • 14
  • 22

9 Answers9

15

you need to write and call 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;            
        }  
      }
   }

reference

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

HelloIT
  • 162
  • 2
  • 17
Asad
  • 20,544
  • 16
  • 68
  • 92
5

And this for clearing all controls in form like textbox, checkbox, radioButton

you can add different types 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;
            }
        }
    }
Chandan Pasunoori
  • 932
  • 1
  • 13
  • 28
0

You may use JavaScript form reset() method or loop throw all textboxes and set Text property to empty string.

foreach(var control in this.Controls){
   TextBox tb = control as TextBox;
   if (tb != null)
   {
      tb.Text = string.Empty;
   }
}
sashaeve
  • 8,993
  • 10
  • 46
  • 60
0

You could do this in the page:

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

If you need to clear a whole hierarchy of them, call this once from the page: ClearTextBoxes(this);

And here's 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
  }
}
Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151
0
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 textboxes in a list and set their textproperty to "".

Jens
  • 3,211
  • 2
  • 24
  • 41
0

Well You can as well set the field to not use viewstate, which will imply in a boost in performance, small , but a boost nonetheless. As well, you could do a response.redirect to the same page, or a server.transfer.
If those two solutions doesn't suit you, you might use something of a textbox.text = string.empty, and dropdowlist.clearselection. They might be not as fast as you might want they are a lot more elegant.

marcelo-ferraz
  • 3,057
  • 4
  • 36
  • 53
0

COMPLETELY UNTESTED SOLUTION:

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

Or it might be Page.Viewstate.Clear() or even Page.ClearChildViewState()...

Sorry - haven't tried it in anger....

CJM
  • 11,780
  • 20
  • 76
  • 115
  • just write this line will reset all fields. this.form1.ID= "newid"; – Adeel Mar 01 '10 at 12:47
  • Could be useful, except where the form ID is referenced (though for obvious reasons, in ASP.NET this tends not to be the case) – CJM Mar 01 '10 at 14:14
0

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

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

Create a function called cleardata():

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

And call this function whenever you need it.

Danilo Valente
  • 11,037
  • 8
  • 52
  • 66
Shrenik
  • 1
  • 1