How to reset all fields of a web form in ASP .NET


Ever wondered whats the easiest way to reset all fields in a web form. well here it is:-

public static void ResetFields(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;

}

ResetFields(contl.Controls);
}
}

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

7 thoughts on “How to reset all fields of a web form in ASP .NET”

  1. We Can reset the ASP.NET page with 2 lines of code

    protected void Button_Reset_Click(object sender, EventArgs e)
    {
    Session[“ViewState”] = null;
    Response.Redirect(“/Roster/DRAC/Create.aspx”);
    }

  2. Both cool methods but I prefer Imran’s, cause it’s easily reusable without modifications and doesn’t need a page reload. 🙂

  3. We Can reset the ASP.NET page with 2 lines of code (better code)

    private void FormReset()
    {
    ViewState.Clear();
    Response.Redirect(Request.Url.AbsoluteUri.ToString());
    }

  4. Thank IMRAN I just only change this:

    ResetFields(contl.Controls);

    For this (to Call the Function):

    ResetFields(this.Form.Controls);

    Sorry, my english isn’t very good.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s