Mr Lonely
Mr Lonely

Reputation: 111

How do I clear all the data in an asp.net page after form submition

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.

Upvotes: 3

Views: 17232

Answers (5)

Rab
Rab

Reputation: 35572

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

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

Upvotes: 3

Asyraf Azmin
Asyraf Azmin

Reputation: 41

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! :)

Upvotes: 1

Omkar Hendre
Omkar Hendre

Reputation: 435

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);
    }

Upvotes: 1

MikeSmithDev
MikeSmithDev

Reputation: 15797

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
}

Upvotes: 3

John Saunders
John Saunders

Reputation: 161773

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.

Upvotes: 0

Related Questions