Resetting Form fields

Last post 10-16-2009 11:58 AM by it.architect. 4 replies.

Sort Posts:

  • Resetting Form fields

    02-01-2007, 3:32 PM
    • Contributor
      2,504 point Contributor
    • jose_jimenez
    • Member since 10-13-2006, 2:43 AM
    • Posts 484

    I posted this on my blog about resetting form fields:

    A coworker and I were working on a form and found it to be quite cumbersome to do it field by field. here is some code we found that works to reset all the fields in a form. Add more types in the case statement to reset more items:


     
    void resetField(object myObj)
        {
            RadioButtonList rl;
            string temp = myObj.GetType().ToString();
            switch (myObj.GetType().ToString())
            {
                case "System.Web.UI.WebControls.TextBox":
                    ((TextBox)myObj).Text = "";
                    break;
                case "System.Web.UI.WebControls.CheckBox":
                    ((CheckBox)myObj).Checked = false;
                    break;
                case "System.Web.UI.WebControls.RadioButtonList":
                    rl = (RadioButtonList)myObj;
                    if (rl.SelectedItem != null)
                        rl.SelectedItem.Selected = false;
                    break;
                default:
                    break;
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Control wc in Page.Form.Controls)
            {
                resetField(wc);
            }
        }
    
    
     

    Please mark as answered if I helped.
    I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
    Filed under:
  • Re: Resetting Form fields

    02-02-2007, 1:04 AM
    • Participant
      1,418 point Participant
    • Zhou
    • Member since 04-29-2006, 8:12 AM
    • Germany
    • Posts 264

    Hi,

    please can you explain me what is the advantage to use this serverside piece of code instead of using the built-in function:
    <input type="reset" ...> ?

    Regards
    Marc Andre

  • Re: Resetting Form fields

    02-06-2007, 8:35 AM
    • Contributor
      2,504 point Contributor
    • jose_jimenez
    • Member since 10-13-2006, 2:43 AM
    • Posts 484

    Honestly, there isn't.  The input type = reset does the job nicely.  If you want to do it after you consume the values on the server, then the code sample might come in handy (since you can't call the same behaivior as the html reset through server code).  Or do you have a solution for that too?

     

     

    Please mark as answered if I helped.
    I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
  • Re: Resetting Form fields

    02-06-2007, 12:11 PM
    • Participant
      1,418 point Participant
    • Zhou
    • Member since 04-29-2006, 8:12 AM
    • Germany
    • Posts 264
    Ok, in this case when you need to consume the values on the serverside before you clear all data the method is helpful. Thanks for your explanation.
  • Re: Resetting Form fields

    10-16-2009, 11:58 AM
    • Member
      12 point Member
    • it.architect
    • Member since 04-16-2009, 6:01 AM
    • Posts 18
    ResetFields(Page.Controls);


    this article's very good

    http://imak47.wordpress.com/2008/08/01/how-to-reset-all-fields-of-a-web-form-in-asp-net/

    it's possible to add other controls like:

     (...)

                    case "CascadingDropDown":
                        AjaxControlToolkit.CascadingDropDown cdd = (AjaxControlToolkit.CascadingDropDown)contl;
                        cdd.SelectedValue = null;
                    break;
    (...)
     
    and the function can be called like this: 
     

     ResetFields(Page.Controls);

Page 1 of 1 (5 items)