How to reset or clear all web form controls in - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to reset or clear all web form controls in

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


HTML Markup:

<fieldset style="width:450px">
    <legend>Reset All controls Example</legend>
        <tr><td>First Name:</td><td>
            <asp:TextBox ID="frstname" runat="server"></asp:TextBox></td></tr>
         <tr><td>Last Name:</td><td>
             <asp:TextBox ID="lastname" runat="server"></asp:TextBox></td></tr>           
                       <asp:CheckBoxList ID="CheckBoxList1" runat="server"
                           RepeatDirection="Horizontal" >
            <asp:RadioButton ID="rblnet" runat="server" Text="" GroupName="a"/>
            <asp:RadioButton ID="rblphp"
                runat="server" Text="Php" GroupName="a" />         
             <asp:DropDownList ID="ddlcity" runat="server">
        <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>
         <tr><td>I accept the term and condition: </td><td>
             <asp:CheckBox ID="chk" runat="server" /></td></tr>       
                  <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />

After insert the record to database write the below given code (C#):
   protected void Button1_Click(object sender, EventArgs e)
    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))
            else if (cntrl.GetType() == typeof(RadioButton))
                ((RadioButton)(cntrl)).Checked = false;
            else if (cntrl.GetType() == typeof(RadioButtonList))


Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    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

    End Sub


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.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.