Asp.Net Clear all controls value reset

Asp.Net Clear all controls value reset

Today, We want to share with you Asp.Net Clear all controls value reset.
In this post we will show you Find Page control and clear its value in Asp.Net, hear for Get Master Page Control Value In Content Page and c# – Clear all controls in a ASP.NET page we will give you demo and example for implement.
In this post, we will learn about reset or clear all asp.net controls/fields on web page/form ? with an example.

Introduction

In this post I will demonstrate how you can find page control and clear control value.
Below I have provided Asp.net markup for several mostly used control. On button click event handler i have wriiten code to clear the value of control.

Example for clear control value

Asp.net markup

<form id="frmMain" runat="server">
<fieldset style="width: 300px">
<legend>Reset CheckBox</legend>
                <asp:CheckBox ID="chkYes" runat="server" Text="Yes" />
                <asp:CheckBox ID="chkNo" runat="server" Text="No" />
            </fieldset>
<fieldset style="width: 300px">
<legend>Reset CheckBoxList</legend>
                <asp:CheckBoxList ID="cbColor" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Red</asp:ListItem>
                    <asp:ListItem>Pink</asp:ListItem>
                    <asp:ListItem>White</asp:ListItem>
                    <asp:ListItem>Green</asp:ListItem>
                </asp:CheckBoxList>
            </fieldset>
<fieldset style="width: 300px">
<legend>Clear TextBox</legend>
                <asp:TextBox ID="txt1" runat="server" Width="140px"></asp:TextBox>
                <asp:TextBox ID="txt2" runat="server" Width="140px"></asp:TextBox>
            </fieldset>
<fieldset style="width: 300px">
<legend>Reset DropDownList</legend>
                <asp:DropDownList ID="ddlColor" runat="server">
                    <asp:ListItem>-- Select --</asp:ListItem>
                    <asp:ListItem>Red</asp:ListItem>
                    <asp:ListItem>Pink</asp:ListItem>
                    <asp:ListItem>White</asp:ListItem>
                    <asp:ListItem>Black</asp:ListItem>
                </asp:DropDownList>

            </fieldset>
<fieldset style="width: 300px">
<legend>Reset RadioButton</legend>
                <asp:RadioButton ID="rb1" runat="server" Text="Radiobutton" />
            </fieldset>
<fieldset style="width: 300px">
<legend>Reset RadioButtonList</legend>
                <asp:RadioButtonList ID="rblFruit" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <asp:ListItem>Apple</asp:ListItem>
                    <asp:ListItem>Orange</asp:ListItem>
                    <asp:ListItem>Banana</asp:ListItem>
                    <asp:ListItem>Mango</asp:ListItem>
                </asp:RadioButtonList>
            </fieldset>
<fieldset style="width: 300px">
<legend>Reset Label Text</legend>
                <asp:Label ID="lblSample1" runat="server" Text="label1"></asp:Label>&nbsp;&nbsp
                <asp:Label ID="lblSample2" runat="server" Text="label2"></asp:Label>
            </fieldset>
            <asp:HiddenField ID="hf1" runat="server" Value="123" />
            
            <asp:Button ID="btnReset" runat="server" Text="Reset controls" OnClick="btnReset_Click" />
        </div>
    </form>

C#.net code to clear control value

 protected void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control _Control in frmMain.Controls)
            {
                //Clear the TextBox content
                if (_Control is TextBox)
                {
                    ((TextBox)(_Control)).Text = string.Empty;
                }
                //Clear the Label text
                else if (_Control is Label)
                {
                    ((Label)(_Control)).Text = string.Empty;
                }
                // Select default item
                else if (_Control is DropDownList)
                {
                    ((DropDownList)(_Control)).ClearSelection();
                }
                //uncheck the selection
                else if (_Control is CheckBox) 
                {
                    ((CheckBox)(_Control)).Checked = false;
                }
                // uncheck all the selection
                else if (_Control is CheckBoxList) 
                {
                    ((CheckBoxList)(_Control)).ClearSelection();
                }
                //uncheck the selection
                else if (_Control is RadioButton) 
                {
                    ((RadioButton)(_Control)).Checked = false;
                }
                // uncheck all the selection
                else if (_Control is RadioButtonList) // or else if (_Control.GetType().Equals(typeof(RadioButtonList)))
                {
                    ((RadioButtonList)(_Control)).ClearSelection();
                }
                //clear the value of HiddenField control
                else if (_Control is HiddenField)
                {
                    ((HiddenField)(_Control)).Value = string.Empty;
                }
            }
        }

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Get Master Page Control Value In Content Page.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Vue 2.0 Hello World Step By Step
  2.   How to Get Current Date & Time in JavaScript
  3.   Decimal Pipe number Filter using Angular 9/8 Example
  4.   Conditional When Case Statement in Laravel 6 Eloquent
  5.   Laravel 6 Update Query using Eloquent Example
  6.   Vuejs Array Values Change Example with Demo
  7.   Laravel Dynamic active menu Example Tutorial
  8.   Laravel Delete Multiple Checkboxes Example
  9.   Clone Specific Git Branch from Remote Repository
  10.   PHP Try Catch Exception & Error Handling Tutorial