How to reset or clear all web form controls in asp.net

Introduction: In this article today I am going to explain how we can reset or clear all form controls in asp.net code behind.

Description:

HTML Markup:

<fieldset style="width:450px">
    <legend>Reset All controls Example</legend>
        <table>
        <tr><td></td><td></td></tr>
        <tr><td>First Name:</td><td>
            <asp:TextBox ID="frstname" runat="server"></asp:TextBox></td></tr>
            <tr><td></td><td></td></tr>
         <tr><td>Last Name:</td><td>
             <asp:TextBox ID="lastname" runat="server"></asp:TextBox></td></tr>           
                <tr><td></td><td></td></tr>
                   <tr><td>Gender:</td><td>          
                       <asp:CheckBoxList ID="CheckBoxList1" runat="server"
                           RepeatDirection="Horizontal" >
                           <asp:ListItem>Male</asp:ListItem>
                           <asp:ListItem>Female</asp:ListItem>
                       </asp:CheckBoxList>         
        </td></tr>
        <tr><td></td><td></td></tr>   
        <tr><td>Technology:</td><td>         
 
            <asp:RadioButton ID="rblnet" runat="server" Text="Asp.net" GroupName="a"/>
            <asp:RadioButton ID="rblphp"
                runat="server" Text="Php" GroupName="a" />         
        </td></tr>
        <tr><td></td><td></td></tr>
         <tr><td>City:</td><td>
             <asp:DropDownList ID="ddlcity" runat="server">
             <asp:ListItem>--Select--</asp:ListItem>
                 <asp:ListItem>Delhi</asp:ListItem>
                 <asp:ListItem>Kolkata</asp:ListItem>
                 <asp:ListItem>Mumbai</asp:ListItem>
             </asp:DropDownList>
         </td></tr>
        <tr><td></td><td></td></tr>
        <tr><td>Job Type:</td><td>
            <asp:RadioButtonList ID="rbljobt" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem>Full Time</asp:ListItem>
                <asp:ListItem>Part Time</asp:ListItem>
            </asp:RadioButtonList>
        </td></tr>
            <tr><td></td><td></td></tr>
         <tr><td>I accept the term and condition: </td><td>
             <asp:CheckBox ID="chk" runat="server" /></td></tr>       
              <tr><td></td><td>
                  <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
                  </td></tr>
        </table>
     </fieldset>

After insert the record to database write the below given code (C#):
   protected void Button1_Click(object sender, EventArgs e)
    {
        ClearControls();
    }
    public void ClearControls()
    {
        foreach (Control cntrl in form1.Controls)
        {
            if (cntrl.GetType() == typeof(TextBox))
            {
                ((TextBox)(cntrl)).Text = "";
            }
         
            else if (cntrl.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(cntrl)).SelectedIndex = 0;
            }
            else if (cntrl.GetType() == typeof(CheckBox))
            {
                ((CheckBox)(cntrl)).Checked = false;
            }
            else if (cntrl.GetType() == typeof(CheckBoxList))
            {
                ((CheckBoxList)(cntrl)).ClearSelection();
            }
            else if (cntrl.GetType() == typeof(RadioButton))
            {
                ((RadioButton)(cntrl)).Checked = false;
            }
            else if (cntrl.GetType() == typeof(RadioButtonList))
            {
                ((RadioButtonList)(cntrl)).ClearSelection();
            }
        }
    }

VB:

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        ClearControls()
    End Sub

    Public Sub ClearControls()
        For Each cntrl As Control In form1.Controls
            If cntrl.[GetType]() = GetType(TextBox) Then
                DirectCast(cntrl, TextBox).Text = ""
            ElseIf cntrl.[GetType]() = GetType(DropDownList) Then
                DirectCast(cntrl, DropDownList).SelectedIndex = 0
            ElseIf cntrl.[GetType]() = GetType(CheckBox) Then
                DirectCast(cntrl, CheckBox).Checked = False
            ElseIf cntrl.[GetType]() = GetType(CheckBoxList) Then
                DirectCast(cntrl, CheckBoxList).ClearSelection()
            ElseIf cntrl.[GetType]() = GetType(RadioButton) Then
                DirectCast(cntrl, RadioButton).Checked = False
            ElseIf cntrl.[GetType]() = GetType(RadioButtonList) Then
                DirectCast(cntrl, RadioButtonList).ClearSelection()
            End If
        Next

    End Sub

Output


Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget