Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows the different ways on how to clear control values in the page. Here are the following approaches that you can use.

1. Creating a method that would loop through the page controls and clear its values accordingly like:

    private void ClearControls()
    {

        foreach (Control c in Page.Controls)
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox tb = (TextBox)c;
                if (tb != null)
                {
                    tb.Text = string.Empty;
                }
            }

            if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                DropDownList ddl = (DropDownList)c;
                if (ddl != null)
                {
                    ddl.ClearSelection();
                }
            }
        }
     }

 

2. Creating a generic Recursive FindControl method that would search all the Controls in the page and reset their values.

    public static void ClearControls(Control Parent)
    {
        if (Parent is TextBox)
        { (Parent as TextBox).Text = string.Empty; }
        else{
            foreach (Control c in Parent.Controls)
                ClearControls(c);
        }
    }

    //Then call the method like this
    ClearControls(Page)

 

3. Using HTML Input type RESET Button

<input type="reset" value="Reset form" />

 

4. Redirect to the same page using Response.Redirect method.

Response.Redirect("YourPage.aspx");

That's it.

PS .If you have any other techniques to clear control values in the page then please let me know so I can list them here. Thanks.

Technorati Tags: ,,
Posted on Wednesday, April 15, 2009 1:10 AM ASP.NET , C# , Tips&Tricks | Back to top


Comments on this post: Ways to Clear Control values in the Page.

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net