Find all ASP.NET loop through controls on Form Clear of specific type

Find all ASP.NET loop through controls on Form Clear of specific type

In this Post We Will Explain About is Find all ASP.NET loop through controls on Form Clear of specific type With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to HOW TO FIND ALL ASP.NET CONTROLS OF SPECIFIC TYPE Example

In this post we will show you Best way to implement Finding all controls in an ASP.NET Panel, hear for Finding all controls of a specific type on a page using ASP.NET with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

how can you loop through all controls on an asp.net web form

While going on livepage it can be list the tag required to perform some an action based perform on the type of the form control. Finding list form input controls now on the some livepage and then clearing list the form elements as well as some data resetting means defult settings their values was required in HTML Form my some case. Here We have provided an some example how We achieved that.

Also Read :  Top 10 Ways to Increase Google Adsense Earnings

here Make a simple livepage and place HTML Elements Like as a checkbox, checkboxlist, HTML textbox, HTML form elements dropdownlist, form elements radiobutton, form elements radiobuttonlist, form elements label as well as some input hiddenfield controls etc.

HTML Source

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="live_form" runat="server">
        <div>
            <fieldset style="width: 280px">
                <legend>Reset CheckBox</legend>
                <asp:CheckBox ID="firstcheckbox" runat="server" Text="firstcheckbox" />
                <asp:CheckBox ID="SecondCheckbox" runat="server" Text="SecondCheckbox" />
            </fieldset>
            <fieldset style="width: 280px">
                <legend>Reset CheckBoxList</legend>
                <asp:CheckBoxList ID="Firstcheckboxlist" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Redmi</asp:ListItem>
                    <asp:ListItem>iphone </asp:ListItem>
                    <asp:ListItem>lenovo </asp:ListItem>
                    <asp:ListItem>Laptop </asp:ListItem>
                </asp:CheckBoxList>
            </fieldset>
            <fieldset style="width: 280px">
                <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: 280px">
                <legend>Reset DropDownList</legend>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>-- Select --</asp:ListItem>
                    <asp:ListItem>Redmi</asp:ListItem>
                    <asp:ListItem>iphone </asp:ListItem>
                    <asp:ListItem>lenovo </asp:ListItem>
                    <asp:ListItem>Laptop </asp:ListItem>
                </asp:DropDownList>

            </fieldset>
            <fieldset style="width: 280px">
                <legend>Reset RadioButton</legend>
                <asp:RadioButton ID="RadioButton1" runat="server" Text="Radiobutton" />
            </fieldset>
            <fieldset style="width: 280px">
                <legend>Reset RadioButtonList</legend>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Redmi</asp:ListItem>
                    <asp:ListItem>iphone</asp:ListItem>
                    <asp:ListItem>lenovo</asp:ListItem>
                    <asp:ListItem>Laptop</asp:ListItem>
                </asp:RadioButtonList>
            </fieldset>
            <fieldset style="width: 280px">
                <legend>Reset Label Text</legend>
                <asp:Label ID="Label1" runat="server" Text="label1 text"></asp:Label> &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="data_clear_reset" />
        </div>
    </form>
</body>
</html>

Asp.Net C# source Code

to find html some controls by their more type and clear values

protected void data_clear_reset(object sender, EventArgs e)
    {
    
        foreach (Control liveCtrl in live_form.Controls)
        {
            
            if (liveCtrl is TextBox)// or if (liveCtrl.GetType().Equals(typeof(TextBox)))
            {
                ((TextBox)(liveCtrl)).Text = string.Empty;
            }
            
            else if (liveCtrl is Label) // or else if (liveCtrl.GetType().Equals(typeof(Label)))
            {
                ((Label)(liveCtrl)).Text = string.Empty;
            }
            
            else if (liveCtrl is DropDownList)// or else if (liveCtrl.GetType().Equals(typeof(DropDownList)))
            {
                ((DropDownList)(liveCtrl)).ClearSelection();
            }
            
            else if (liveCtrl is CheckBox) // or else if (liveCtrl.GetType().Equals(typeof(CheckBox)))
            {
                ((CheckBox)(liveCtrl)).Checked = false;
            }
            
            else if (liveCtrl is CheckBoxList) // or else if (liveCtrl.GetType().Equals(typeof(CheckBoxList)))
            {
                ((CheckBoxList)(liveCtrl)).ClearSelection();
            }
            
            else if (liveCtrl is RadioButton) // or else if (liveCtrl.GetType().Equals(typeof(RadioButton)))
            {
                ((RadioButton)(liveCtrl)).Checked = false;
            }
          
            else if (liveCtrl is RadioButtonList) // or else if (liveCtrl.GetType().Equals(typeof(RadioButtonList)))
            {
                ((RadioButtonList)(liveCtrl)).ClearSelection();
            }
            
            else if (liveCtrl is HiddenField) //or else if (liveCtrl.GetType().Equals(typeof(HiddenField)))
            {
                ((HiddenField)(liveCtrl)).Value = string.Empty;
            }
        }
    }

Also Read :  How to Get List all Laravel routes

Asp.Net VB Source Code

to list the find some controls by their list the type and clear values

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

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me feedBackMore Details……
Angularjs Example

Also Read :  jquery replace newline with br Example - Convert new lines to br - nl2br function

Example

I hope you have Got What is HOW TO FIND ALL ASP.NET CONTROLS OF SPECIFIC TYPE AND CLEAR THEIR VALUES And how it works.I would Like to have FeaeBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *