We help IT Professionals succeed at work.

clearing form fields after postback

BrijBhasin
BrijBhasin asked
on
2,160 Views
Last Modified: 2010-05-18
Is there a way to clear all the the form fields i.e textbox, dropdownlist  after a postback event like button click
Comment
Watch Question

Top Expert 2005

Commented:

Author

Commented:
I know about the IsPostBack Property but how do I clear the form fields?

Commented:
1. turn off the viewstate, or
2. Response.Redirect(Request.RawURL) in your event handler
Software Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I get an error "Value of an integral type expected" here  --  switch (ctrl.GetType())

Commented:
adding a ToString() to the GetType() should do the trick, i reckon...

Author

Commented:
that removed the error but the form fields don't get cleared.  BTW this is for a web form so I changed the code as following and that didn't help either

foreach (object ctrl in Page.Controls)
                  {

                        switch (ctrl.GetType().ToString())
                        {      
                              case "System.Web.UI.WebControls.TextBox":
                                    ((TextBox)ctrl).Text = "";
                                    break;
//                              case "System.Windows.Forms.ComboBox":
//                                    ((ComboBox)ctrl).Items.Clear();
//                                    break;
                              default:

                                    break;
                                    // etcetera
                        }
                  }

Commented:
did you try just redirecting yourself back to the same page?

Author

Commented:
No I haven't tried that as my page has several panels where the visibilty is controlled by user selections .. if I do a redirect I will lose the panel the user was working on.

Commented:
can you reload just the panel with the input fields in it then?

Author

Commented:
ya I could load the panel and clear the fields individually but I thought I could have a more universal solution which I could use accross my application.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Following is a modified working version of a code snippet I found online ..  similar to eliytres suggest .. I pass a Panel to this function

public static void ClearFormFields(System.Web.UI.Control parent)
            {
                  //where parent is a panel;
                  foreach (Control c in parent.Controls)
                  {
                        if ((c.GetType() == typeof(TextBox)))
                        {
                              // is it a textbox?            
                              TextBox t = (TextBox)c;
                              t.Text = "";
                        }
                        else if ((c.GetType() == typeof(DropDownList)))
                        {
                              // is it a dropdown list?              
                              DropDownList d = (DropDownList)c;
                              d.ClearSelection();
                        }
                        else if ((c.GetType() == typeof(ListBox)))
                        {
                              // is it a listbox?                
                              ListBox l = (ListBox)c;              
                              l.ClearSelection();
                        }
                        else if ((c.GetType() == typeof(RadioButtonList)))
                        {
                              // is it a radiobutton list?    
                              RadioButtonList rl = (RadioButtonList)c;
                              rl.ClearSelection();
                        }
                        else if ((c.GetType() == typeof(CheckBox)))
                        {
                              // is it a checkbox?            
                              CheckBox chk = (CheckBox)c;
                              chk.Checked = false;
                        }
                        else if ((c.GetType() == typeof(CheckBoxList)))
                        {
                              // is it a radiobutton list?    
                              CheckBoxList cl = (CheckBoxList)c;
                              cl.ClearSelection();
                        }
//                        if (c.HasControls())
//                        {
//                              ClearFields(c);
//                        }
                  }
            }

Author

Commented:
sorry I meant to say jaime_olivares suggestion
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.