• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

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

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
heyne230
Asked:
heyne230
1 Solution
 
UnexplainedWaysCommented:
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
 
thaytu888888Commented:
- 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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now