Link to home
Start Free TrialLog in
Avatar of BrijBhasin
BrijBhasin

asked on

clearing form fields after postback

Is there a way to clear all the the form fields i.e textbox, dropdownlist  after a postback event like button click
Avatar of Erick37
Erick37
Flag of United States of America image

Avatar of BrijBhasin
BrijBhasin

ASKER

I know about the IsPostBack Property but how do I clear the form fields?
1. turn off the viewstate, or
2. Response.Redirect(Request.RawURL) in your event handler
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get an error "Value of an integral type expected" here  --  switch (ctrl.GetType())
adding a ToString() to the GetType() should do the trick, i reckon...
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
                        }
                  }
did you try just redirecting yourself back to the same page?
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.
can you reload just the panel with the input fields in it then?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
//                        }
                  }
            }
sorry I meant to say jaime_olivares suggestion