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,419
  • 5
  • 44
  • 69
Sheery
  • 1,123
  • 5
  • 14
  • 22

9 Answers9

16

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
  • 172
  • 2
  • 22
Asad
  • 21,468
  • 17
  • 69
  • 94
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
  • 934
  • 1
  • 14
  • 31
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
  • 9,387
  • 10
  • 48
  • 61
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
  • 623,446
  • 136
  • 1,297
  • 1,155
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,249
  • 2
  • 25
  • 42
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,147
  • 4
  • 38
  • 55
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,908
  • 20
  • 77
  • 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,270
  • 8
  • 53
  • 67
Shrenik
  • 1
  • 1