Clear controls value using Javascript


Clear states of all controls using Javascript

I am creating a record insert page with textboxes and dropdowns.

When the user clicks "Add", I hope to display a insertion summary
on the top of the page and clear all
fields value using server side

 HtmlForm form = (HtmlForm) this.FindControl("Form1");

            foreach (Control ctrl in form.Controls)

            {

                if (ctrl is TextBox)

                    ((TextBox) ctrl).Text = string.Empty;

                else if (ctrl is DropDownList)

                    ((DropDownList)ctrl).SelectedIndex = 0;

            }



fields value using client  side means Using Javascript

 <script language="javascript" type='text/javascript'> 
        function ClearAllControls()
        {
              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;
                                break;                     

                          case "select-multiple" :
                                while (doc.selectedIndex != -1)
                                {
                                      indx = doc.selectedIndex;
                                      doc.options[indx].selected = false;
                                }
                                doc.selected = false;
                                break;                                   
                          default :
                                break;
                    }
              }
        }
</script>

Ebook Download
View all
Learn
View all