Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing all fields in an asp.net form

Tags:

c#

asp.net

I have an asp.net form. In this formI want to clear all the data what I entered in textbox,dropdownlist etc. So how can I do without going to each textbox and set the value.

like TextBox1.Text=""; etc. How can i clear all values of a form ?

like image 526
kbvishnu Avatar asked Nov 27 '22 15:11

kbvishnu


1 Answers

Either

Use this function

private void ClearInputs(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        else if (ctrl is DropDownList)
            ((DropDownList)ctrl).ClearSelection();

        ClearInputs(ctrl.Controls);
    }
}

and call it like

ClearInputs(Page.Controls);

or

Redirect to the same page

Response.Redirect(Request.Url.PathAndQuery, true);
like image 54
naveen Avatar answered Dec 05 '22 01:12

naveen