Please disable your adblock and script blockers to view this page
 

How to clear or reset all asp.net controls/fields on web page/form

Introduction: In this article i am going to explain How to clear or reset all the asp.net controls/fields e.g. all TextBox, all Label, all CheckBox, all CheckBoxList, all RadioButton, all RadioButtonList, all DropDownList etc that are present on the web page/form. 
Clear or reset asp.net controls
Click on the image to enlarge
In previous articles i explained How to Prevent duplicate record entry on page refresh and Retain password value in the asp.net TextBox after postback event and Count and display remaining characters in the multiline textbox and Show jQuery tooltip message on mouse over on asp.net controls and Remove duplicate records from Sql Server database table.

Description: On submitting the forms e.g. registration form, contact us form etc developers wants to clear/reset the filled controls on the form so that they become ready to be filled again. But if there are large numbers of controls on the form then it becomes time consuming task to clear or reset each controls. So i decided to share the trick to clear or reset all the controls by an easy way that requires less code.
The trick is to loop through all the controls present on the page and check for the type of the control e.g. if the control is textbox then clear the value of that. Similarly for other control, checking the control type and clearing or resetting the control.

Implementation: Let's create an asp.net web application to check this functionality in action.
In the design page(.aspx) place multiple controls e.g. TextBox, Label, CheckBox, CheckBoxList, RadioButton, RadioButtonList, DropDwonList etc that are present on the web page as:

HTML Source Code

<div>
    <fieldset style="width:430px;">
    <legend>Clear/Reset all control on asp.net web page/form</legend>   
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Checkbox sample" /><br />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2">
            <asp:ListItem>sample item1 in checkboxlist</asp:ListItem>
            <asp:ListItem>sample item2 in checkboxlist</asp:ListItem>
            <asp:ListItem>sample item3 in checkboxlist</asp:ListItem>
            <asp:ListItem>sample item4 in checkboxlist</asp:ListItem>
        </asp:CheckBoxList>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>-- Select One --</asp:ListItem>
            <asp:ListItem>sample item1 in dropdownlist</asp:ListItem>
            <asp:ListItem>sample item2 in dropdownlist</asp:ListItem>
            <asp:ListItem>sample item3 in dropdownlist</asp:ListItem>
            <asp:ListItem>sample item4 in dropdownlist</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:RadioButton ID="RadioButton1" runat="server" Text="Radiobutton sample" /><br />   
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="2">
            <asp:ListItem>sample item1 in radiobuttonlist</asp:ListItem>
            <asp:ListItem>sample item2 in radiobuttonlist</asp:ListItem>
            <asp:ListItem>sample item3 in radiobuttonlist</asp:ListItem>
            <asp:ListItem>sample item4 in radiobuttonlist</asp:ListItem>
        </asp:RadioButtonList>
        <br />
    <asp:Label ID="Label1" runat="server" Text="sample text in label1"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Text="sample text in label2"></asp:Label><br />
        <asp:Button ID="btnReset" runat="server" 
            Text="Reset controls" onclick="btnReset_Click" />
            </fieldset>
    </div>

Asp.Net C# Code to reset or clear asp.net controls
  • In the code behind file (.aspx.cs) write the following code on click event of Reset Controls button as.
protected void btnReset_Click(object sender, EventArgs e)
    {
        //Loop through all the control present on the web page/form        
        foreach (Control ctrl in form1.Controls)
        {
            //check for all the TextBox controls on the page and clear them
            if (ctrl.GetType() == typeof(TextBox))
            {
                ((TextBox)(ctrl)).Text = string.Empty;
            }
            //check for all the Label controls on the page and clear them
            else if (ctrl.GetType() == typeof(Label))
            {
                ((Label)(ctrl)).Text = string.Empty;
            }
            //check for all the DropDownList controls on the page and reset it to the very first item e.g. "-- Select One --"
            else if (ctrl.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(ctrl)).SelectedIndex = 0;
            }
            //check for all the CheckBox controls on the page and unchecked the selection
            else if (ctrl.GetType() == typeof(CheckBox))
            {
                ((CheckBox)(ctrl)).Checked = false;
            }
            //check for all the CheckBoxList controls on the page and unchecked all the selections
            else if (ctrl.GetType() == typeof(CheckBoxList))
            {
                ((CheckBoxList)(ctrl)).ClearSelection();
            }
            //check for all the RadioButton controls on the page and unchecked the selection
            else if (ctrl.GetType() == typeof(RadioButton))
            {
                ((RadioButton)(ctrl)).Checked = false;
            }
            //check for all the RadioButtonList controls on the page and unchecked the selection
            else if (ctrl.GetType() == typeof(RadioButtonList))
            {
                ((RadioButtonList)(ctrl)).ClearSelection();
            }
        }
    }

Asp.Net VB Code to reset or clear asp.net controls
  • Design the form as in HTML Source above but replace the line         
<asp:Button ID="btnReset" runat="server"   Text="Reset controls" onclick="btnReset_Click" />
with the following  line
         <asp:Button ID="btnReset" runat="server"   Text="Reset controls" /> 
  • Then in the code behind file (.aspx.vb) write the following code on click event of Reset Controls button as.
Protected Sub btnReset_Click(sender As Object, e As System.EventArgs) Handles btnReset.Click
        'Loop through all the control pressent on the web page/form        
        For Each ctrl As Control In form1.Controls
            'check for all the TextBox controls on the page and clear them
            If ctrl.[GetType]() = GetType(TextBox) Then
                DirectCast(ctrl, TextBox).Text = String.Empty
                'check for all the Label controls on the page and clear them
            ElseIf ctrl.[GetType]() = GetType(Label) Then
                DirectCast(ctrl, Label).Text = String.Empty
                'check for all the DropDownList controls on the page and reset it to the very first item e.g. "-- Select One --"
            ElseIf ctrl.[GetType]() = GetType(DropDownList) Then
                DirectCast(ctrl, DropDownList).SelectedIndex = 0
                'check for all the CheckBox controls on the page and unchecked the selection
            ElseIf ctrl.[GetType]() = GetType(CheckBox) Then
                DirectCast(ctrl, CheckBox).Checked = False
                'check for all the CheckBoxList controls on the page and unchecked all the selections
            ElseIf ctrl.[GetType]() = GetType(CheckBoxList) Then
                DirectCast(ctrl, CheckBoxList).ClearSelection()
                'check for all the RadioButton controls on the page and unchecked the selection
            ElseIf ctrl.[GetType]() = GetType(RadioButton) Then
                DirectCast(ctrl, RadioButton).Checked = False
                'check for all the RadioButtonList controls on the page and unchecked the selection
            ElseIf ctrl.[GetType]() = GetType(RadioButtonList) Then
                DirectCast(ctrl, RadioButtonList).ClearSelection()
            End If
        Next
    End Sub

Now over to you:
" I hope you have got the way to clear or reset asp.net control in one go and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

5 comments

Click here for comments
March 08, 2014 ×

Thanks ipsita pani..it is always nice to hear that my article helped anyone..Stay connected and keep reading for more useful updates like this one..:)

Reply
avatar
lopa
admin
August 13, 2014 ×

Thanks for this code. :)

Reply
avatar
August 13, 2014 ×

your welcome ..keep reading for more useful updates

Reply
avatar
Anonymous
admin
October 13, 2014 ×

You are very generous people... not suspectable...!!!

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon