Home > ASP.Net, Javascript > Reset Controls in Web Form

Reset Controls in Web Form


Hi

try this example to reset all controls in web page using javascript

Salient features:

  1. Using this code, you can reset HTML controls as well as ASP controls like text box, text area, radio buttons, etc.
  2. This function can reset the File Upload control as well.
  3. Using this, we can reset .NET Validation Controls too, like RequiredFieldValidater, etc.
  4. This code has no issue with any browser like FireFox, Internet Explorer, Google Chrome, Safari, etc…
<script type="text/javascript">
      
        //function for reset controls
        function ClearAllControls()
        {
            //set your validation controls prefix here.
            var validationControlsPrefix = "req";
            resetmsg(validationControlsPrefix);
              for (i=0; i<document.forms[0].length; 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;
                                doc.selectedIndex = 0;
                                break;                    
                          case "select-multiple" :
                                while (doc.selectedIndex != -1)
                                {
                                      indx = doc.selectedIndex;
                                      doc.options[indx].selected = false;
                                }
                                doc.selected = false;
                                break;
                         case "textarea":
                                doc.value = "";
                                break;
                          case "file" :
                              var browser=navigator.appName;
                             if(browser == 'Microsoft Internet Explorer')
                              {
                                 var fil = doc;
                                 //fil.select();
                                 n=fil.createTextRange();
                                 n.execCommand('delete');                         
                              }
                              else
                              {
                                 doc.value='';
                              }
                             break; 
                          default :
                                break;
                    }
              }
        }
      
        //function for reset validation controls
        function resetmsg(validationControlsPrefix)
        {
         
           var spans;
             var browser=navigator.appName;
             if(browser == 'Microsoft Internet Explorer')
              {
                spans = document.all.tags('span');
              }
              else
              {
                spans =  document.getElementsByTagName('span');
              }
            
            if (spans)
             {
                for (var i = 0; i < spans.length; i++)
                 {
                    var prefixLength = "" + validationControlsPrefix.length;
                    var currID = "" + spans[i].id
                    if ((currID != '') && (prefixLength != ''))
                       {
                          if (currID.substring(0,prefixLength) ==
     validationControlsPrefix)
                            {
                                spans[i].style.display = "none";
                             }
                       }
                 }
             }         
          
        }      
        </script> 

Hope  this helps

Good Luck.

Advertisements
Categories: ASP.Net, Javascript
  1. reed1
    August 20, 2010 at 2:58 am

    I was looking for a way to reset the form partially and this worked perfectly , thanks man

  2. reed1
    August 20, 2010 at 9:19 am

    one more sir, you forgot to add case “hidden” 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: