Solved

How to clear all the controls of an aspx page(form)?

Posted on 2010-08-20
3
482 Views
Last Modified: 2013-12-17
How to clear all the controls of an aspx page
My page has combo boxes , text boxes , radio buttons etc
Client side:
i tried
document.forms[0].reset();
Did not work as its supposed to only for html pages

Server side:
this.form1.Controls.Clear();
the above statement gave me an error. y?

What is the solution using client side javascripting?
AND also if we have to do this from server side?
0
Comment
Question by:heyne230
3 Comments
 
LVL 12

Expert Comment

by:UnexplainedWays
Comment Utility
are you using any js libs (moo / jq), if so you could do a simple select from the form and get all input/select/checkbox's and then set them back to default values.

You could do it from the code behind and iterate recursively and find all the controls that are the same type and also reset.

Are you wanting to reset on a js action, or when the form posts and you want it to be cleared?
0
 
LVL 6

Accepted Solution

by:
thaytu888888 earned 500 total points
Comment Utility
- Try codes below:
var frm_elements = yourFormName.elements;

for(i=0; i<frm_elements.length; i++) {

field_type = frm_elements[i].type.toLowerCase();

switch(field_type) {

case "text":
case "password":
case "textarea":
case "hidden":

elements[i].value = "";
break;

case "radio":
case "checkbox":

if (elements[i].checked) {

elements[i].checked = false;

}
break;

case "select-one":
case "select-multi":

elements[i].selectedIndex = -1;
break;

default:
break;
}
}

Open in new window

0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now