Solved

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

Posted on 2010-08-20
3
483 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
ID: 33489893
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
ID: 33490115
- 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
ID: 33490854
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…

861 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

25 Experts available now in Live!

Get 1:1 Help Now