3

I've written a lengthy asp.net webform that has about 44 controls. And this data gets saved into a DB. My problem is, after form submit, I would like to clear ALL the data in the ViewState and the webform controls' contents should be cleared. How is it possible without manually(and tediously) clear each control?

ViewState.Clear() does not work Page.EnableViewState = false does not work.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mr Lonely
  • 111
  • 2
  • 2
  • 3
  • 1
    Note that, if you redirect to another page after the submit, then you don't have a problem. This is only an issue in the case where you stay on the page after submit. – John Saunders Dec 14 '12 at 21:13

5 Answers5

3

after insertion is completed, just use Response.Redirect to reload the same page from scratch.

for example Page.Response.Redirect(Page.Request.RawUrl)

Muhammad Saqib
  • 2,185
  • 3
  • 35
  • 48
Raab
  • 34,778
  • 4
  • 50
  • 65
3

If you are staying on the same page, clearing it on the client-side or from the code-behind on the postback would be slightly preferable to a redirect, as you are saving a trip to the server, although it will take more work.

Also, Response.Redirect(url) throws a ThreadAbortionException, which has a negative effect on performance, so if you want to go the redirect route, consider Response.Redirect(url, false).

Client-side option: (the easy way)

<script>
        $(':input').each(function () {
            switch (this.type) {
                case 'password':
                case 'text':
                case 'select-multiple':
                case 'select-one':
                case 'textarea':
                    $(this).val('');
                    break;
                case 'checkbox':
                case 'radio':
                    this.checked = false;
                    break;
            }
        });
</script>

Code pilfered from this post.

Server-side option:

You could loop through all the controls to clear them out. At the end of the function that processes your form, add:

ClearForm(Page.Form.Controls);

The function:

 public void ClearForm(ControlCollection controls)
    {
        foreach (Control c in controls)
        {
            if (c.GetType() == typeof(System.Web.UI.WebControls.TextBox))
            {
                System.Web.UI.WebControls.TextBox t = (System.Web.UI.WebControls.TextBox)c;
                t.Text = String.Empty;
            }
            //... test for other controls in your forms DDL, checkboxes, etc.

            if (c.Controls.Count > 0) ClearForm(c.Controls);
        }
    }

Looping through Controls and child controls is something that comes up a lot, so you could write an extension method to do this. Something along the lines of what I did in this post (but instead a function that instead returns a collection of all the Controls). I have an extension method in my project that does this, called GetAllChildren(), so the same code above would be executed like this:

foreach (Control i in Page.Form.GetAllChildren())
{   
     if (i.GetType() == typeof(System.Web.UI.WebControls.TextBox))
     {
          System.Web.UI.WebControls.TextBox t = (System.Web.UI.WebControls.TextBox)i;
          t.Text = String.Empty;
     }
     // check other types
}
Community
  • 1
  • 1
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
1

Try this

   public static void ClearFields(ControlCollection pageControls)
    {
        foreach (Control contl in pageControls)
        {
            string strCntName = (contl.GetType()).Name;

            switch (strCntName)
            {
                case "TextBox":
                    TextBox tbSource = (TextBox)contl;
                    tbSource.Text = "";
                    break;
                case "RadioButtonList":
                    RadioButtonList rblSource = (RadioButtonList)contl;
                    rblSource.SelectedIndex = -1;
                    break;
                case "DropDownList":
                    DropDownList ddlSource = (DropDownList)contl;
                    ddlSource.SelectedIndex = -1;
                    break;
                case "ListBox":
                    ListBox lbsource = (ListBox)contl;
                    lbsource.SelectedIndex = -1;
                    break;
            }
            ClearFields(contl.Controls);
        }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        ClearFields(Form.Controls);
    }
Omkar Hendre
  • 397
  • 3
  • 12
1

Sorry, I cannot add a comment for Omkar Hendre answer due to my low reputation. The code are good and for my problem, I need to put Page before the Form.Controls.

ClearFields(Page.Form.Controls);

By the way, thank you very much! :)

0

I suggest that you don't play with ViewState. It is meant to properly match the state of the controls.

Instead, just change the state of the controls, either by redirecting, or by clearing the controls explicitly.

John Saunders
  • 160,644
  • 26
  • 247
  • 397