Resetting all controls in an asp.net form

No.of Views558
Bookmarked0 times
Downloads 
Votes0
By  Prajeesh   On  16 May 2010 06:05:08
Tag : ASP.NET , ASP.NET Controls
Resetting all controls in an asp.net form
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In some applications we may need to reset all controls in using a single "Reset" button click, here is the c# code to achieve this.

public  static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)
    {
        foreach (Control cntrl in pagecontrols)
        {
            foreach (Control mycontrols in cntrl.Controls)
            {
                if (txtbox)
                {
                    if (mycontrols is TextBox)
                    {
                        (mycontrols as TextBox).Text = string.Empty;
                    }
                }
                if (dropdownlist)
                {
                    if (mycontrols is DropDownList)
                    {
                        (mycontrols as DropDownList).SelectedIndex = 0;
                    }
                }
                if (label)
                {
                    if (mycontrols is Label)
                    {
                        (mycontrols as Label).Text = string.Empty;
                    }
                }
            }
        }
    }

We can call this function using following format if you want to clear all controls except label

FormControl.ResetControls(this.Controls, true, true, false);

I hope this is help to you all and save time to clear all controls in asp.net form.

 

 
Sign Up to vote for this article
 
About Author
 
Prajeesh
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-India
Joined date-15 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top