Clear all the control values in webpage

Posted by Venkat | Labels:

Here is the code to clear all the control values in the page. this function should call on Reset button Click event.


private void ResetFormValues(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
ResetFormValues(c);
}
else
{
switch(c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;

}
}
}
}

The same you can do it on JAVASCRIPT

Ex:

<input id="Button1" type='button' onclick='ClearAllControls()' value='Clear All Controls Using Javascript' />


<script language="javascript" type='text/javascript'>
function ClearAllControls() {
for (i = 0; i <>
doc = document.forms[0].elements[i];
switch (doc.type) {
case "text":
doc.value = "";
break;
case "checkbox":
doc.checked = false;
break;
case "radio":
doc.checked = false;
break;
case "select-one":
doc.options[doc.selectedIndex].selected = false;
break;
case "select-multiple":
while (doc.selectedIndex != -1) {
indx = doc.selectedIndex;
doc.options[indx].selected = false;
}
doc.selected = false;
break;
default:
break;
}
}
}
</script>

10 comments:

  1. Unknown said...:

    How to call this on my reset button?

  1. Venkat said...:

    If you want to call this method on the button ResetFormValues()

    in C#.Net

    ResetFormValues(this);

  1. Anonymous said...:

    this doesn't work with checkboxlist

  1. Venkat said...:

    Check this link

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=78

  1. Anonymous said...:

    I tried

    if (c.Controls.Count > 0)
    {
    ResetFormValues(c);
    }
    else
    {
    switch (c.GetType().ToString())
    {
    case "System.Web.UI.WebControls.TextBox":
    ((TextBox)c).Text = "";
    birthdate.Text = "mm/dd/yyyy";
    break;
    case "System.Web.UI.WebControls.CheckBoxList":
    CheckBoxList check = (CheckBoxList)c;
    for (var i = 0; i < check.Items.Count; i++)
    {
    check.Items[i].Selected = false;
    }
    break;
    case "System.Web.UI.WebControls.RadioButton":
    RadioButtonList radio = (RadioButtonList)c;
    for (var i = 0; i < radio.Items.Count; i++)
    {
    radio.Items[i].Selected = false;
    }
    break;
    }
    }

    but it also doesn't work.. Could you post some about checkboxlist and radiobuttonlist?

  1. Venkat said...:

    I will post once i workout those samples.

    Have you tried the Javascript on that link.

  1. Anonymous said...:

    I want to do it server side

  1. Venkat said...:

    Ok try to make the EnableViewState=False for the Checkboxlist and put one button after check some items Click the button is it still checkbox shows the Checked one or not..

  1. Anonymous said...:

    It still shows the checked one even I set it to enableviewstate=false

  1. Venkat said...:

    You may found the solution here

    http://forums.asp.net/t/1588464.aspx

Post a Comment

Thanks for the Comments.

PayOffers.in