Please disable your adblock and script blockers to view this page
 

How to find all asp.net controls of specific type and clear their values.

How to find all asp.net controls of specific type and clear their values.Introduction: In this article I am going to explain How to find all asp.net controls of specific type placed on the web page/form. I have created an example of finding all of the control by their type and clearing or resetting their values.


Description: While working on page it may be required to perform an action based on the type of the control. Finding all controls present on the page and clearing or resetting their values was required in my case. Here I have  provided an example how I achieved that.


Implementation: Let’s create a page and place checkbox, checkboxlist, textbox, dropdownlist, radiobutton, radiobuttonlist, label and hiddenfield controls etc. 

HTML Source

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width: 300px">
                <legend>Reset CheckBox</legend>
                <asp:CheckBox ID="CheckBox1" runat="server" Text="Checkbox1" />
                <asp:CheckBox ID="CheckBox2" runat="server" Text="Checkbox2" />
            </fieldset>
            <fieldset style="width: 300px">
                <legend>Reset CheckBoxList</legend>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Item1</asp:ListItem>
                    <asp:ListItem>Item2 </asp:ListItem>
                    <asp:ListItem>Item3 </asp:ListItem>
                    <asp:ListItem>Item4 </asp:ListItem>
                </asp:CheckBoxList>
            </fieldset>
            <fieldset style="width: 300px">
                <legend>Clear TextBox</legend>
                <asp:TextBox ID="TextBox1" runat="server" Width="140px"></asp:TextBox>
                <asp:TextBox ID="TextBox2" runat="server" Width="140px"></asp:TextBox>
            </fieldset>
            <fieldset style="width: 300px">
                <legend>Reset DropDownList</legend>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>-- Select --</asp:ListItem>
                    <asp:ListItem>Item1</asp:ListItem>
                    <asp:ListItem>Item2 </asp:ListItem>
                    <asp:ListItem>Item3 </asp:ListItem>
                    <asp:ListItem>Item4 </asp:ListItem>
                </asp:DropDownList>

            </fieldset>
            <fieldset style="width: 300px">
                <legend>Reset RadioButton</legend>
                <asp:RadioButton ID="RadioButton1" runat="server" Text="Radiobutton" />
            </fieldset>
            <fieldset style="width: 300px">
                <legend>Reset RadioButtonList</legend>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Item1</asp:ListItem>
                    <asp:ListItem>Item2</asp:ListItem>
                    <asp:ListItem>Item3</asp:ListItem>
                    <asp:ListItem>Item4</asp:ListItem>
                </asp:RadioButtonList>
            </fieldset>
            <fieldset style="width: 300px">
                <legend>Reset Label Text</legend>
                <asp:Label ID="Label1" runat="server" Text="label1 text"></asp:Label>&nbsp;&nbsp
                <asp:Label ID="Label2" runat="server" Text="label2 text"></asp:Label>
            </fieldset>
            <asp:HiddenField ID="HiddenField1" runat="server" Value="123" />
            <br />
            <asp:Button ID="btnReset" runat="server"
                Text="Reset controls" OnClick="btnReset_Click" />
        </div>
    </form>
</body>
</html>

Asp.Net C# Code to find controls by their type and clear values

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 TextBox controls on the page and clear them
            if (ctrl is TextBox)// or if (ctrl.GetType().Equals(typeof(TextBox)))
            {
                ((TextBox)(ctrl)).Text = string.Empty;
            }
            //check for all Label controls on the page and clear them
            else if (ctrl is Label) // or else if (ctrl.GetType().Equals(typeof(Label)))
            {
                ((Label)(ctrl)).Text = string.Empty;
            }
            //check for all DropDownList controls on the page and reset them
            else if (ctrl is DropDownList)// or else if (ctrl.GetType().Equals(typeof(DropDownList)))
            {
                ((DropDownList)(ctrl)).ClearSelection();
            }
            //check for all CheckBox controls on the page and unchecked the selection
            else if (ctrl is CheckBox) // or else if (ctrl.GetType().Equals(typeof(CheckBox)))
            {
                ((CheckBox)(ctrl)).Checked = false;
            }
            //check for all CheckBoxList controls on the page and unchecked all the selections
            else if (ctrl is CheckBoxList) // or else if (ctrl.GetType().Equals(typeof(CheckBoxList)))
            {
                ((CheckBoxList)(ctrl)).ClearSelection();
            }
            //check for all RadioButton controls on the page and unchecked the selection
            else if (ctrl is RadioButton) // or else if (ctrl.GetType().Equals(typeof(RadioButton)))
            {
                ((RadioButton)(ctrl)).Checked = false;
            }
            //check for all RadioButtonList controls on the page and unchecked the selection
            else if (ctrl is RadioButtonList) // or else if (ctrl.GetType().Equals(typeof(RadioButtonList)))
            {
                ((RadioButtonList)(ctrl)).ClearSelection();
            }
            //check for all hiddenfield controls on the page and clear them
            else if (ctrl is HiddenField) //or else if (ctrl.GetType().Equals(typeof(HiddenField)))
            {
                ((HiddenField)(ctrl)).Value = string.Empty;
            }
        }
    }

Asp.Net VB Code to find controls by their type and clear values

Protected Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        'Loop through all the control present on the web page/form
        For Each ctrl As Control In form1.Controls
            'check for all TextBox controls on the page and clear them
            If TypeOf ctrl Is TextBox Then
                DirectCast(ctrl, TextBox).Text = String.Empty
                'check for all Label controls on the page and clear them
            ElseIf TypeOf ctrl Is Label Then
                DirectCast(ctrl, Label).Text = String.Empty
                'check for all DropDownList controls on the page and reset them
            ElseIf TypeOf ctrl Is DropDownList Then
                DirectCast(ctrl, DropDownList).ClearSelection()
                'check for all CheckBox controls on the page and unchecked the selection
            ElseIf TypeOf ctrl Is CheckBox Then
                DirectCast(ctrl, CheckBox).Checked = False
                'check for all CheckBoxList controls on the page and unchecked all the selections
            ElseIf TypeOf ctrl Is CheckBoxList Then
                DirectCast(ctrl, CheckBoxList).ClearSelection()
                'check for all RadioButton controls on the page and unchecked the selection
            ElseIf TypeOf ctrl Is RadioButton Then
                DirectCast(ctrl, RadioButton).Checked = False
                'check for all RadioButtonList controls on the page and unchecked the selection
            ElseIf TypeOf ctrl Is RadioButtonList Then
                DirectCast(ctrl, RadioButtonList).ClearSelection()
                'check for all hiddenfield controls on the page and clear them
            ElseIf TypeOf ctrl Is HiddenField Then
                DirectCast(ctrl, HiddenField).Value = String.Empty
            End If
        Next
    End Sub

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin 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 »

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