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.
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.
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.
Add this to the server-side handler of the cancel button:
Response.Redirect("~/mypage.aspx", false);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With