Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear all fields in ASP.net form

Tags:

Is there an easy way to reset all the fields in a form?

I have around 100 controls in my asp.net form and there are submit and reset buttons.

How do I make all values in the fields null when user hits reset button?

I have a lot of dropdown boxes, textboxes, checkboxes.

like image 437
acadia Avatar asked Sep 25 '10 14:09

acadia


People also ask

How do you clear all fields in form?

Complete HTML/CSS Course 2022 In the HTML forms, we use the <input> tag to take user input. To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do you clear fields after form submit in asp net?

Once the OK button of the JavaScript Alert Message Box is clicked, the form fields (data) is cleared (reset) by redirecting to the same page using JavaScript window. location function.


2 Answers

Add this to the server-side handler of the cancel button:

Response.Redirect("~/mypage.aspx", false); 
like image 141
IrishChieftain Avatar answered Oct 10 '22 11:10

IrishChieftain


Loop through all of the controls on the page, and if the control is type TextBox, set the Text property to String.Empty

protected void ClearTextBoxes(Control p1) {     foreach (Control ctrl in p1.Controls)     {         if(ctrl is TextBox)         {              TextBox t = ctrl as TextBox;               if(t != null)              {                   t.Text = String.Empty;              }         }         else        {            if (ctrl.Controls.Count > 0)            {                ClearTextBoxes(ctrl);            }         }     } } 

Then to call it in your click event like this:

 ClearTextBoxes(Page); 
like image 35
TheGeekYouNeed Avatar answered Oct 10 '22 12:10

TheGeekYouNeed