Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

How to clear a Multiple TextBox values in a single click in C# .NET

, 22 Sep 2011
Rate this:
Please Sign up or sign in to vote.
A generic text clear method for those ASP.NET controls which implement the ITextControl interface:private void Clear(ControlCollection controlCollection) where T : ITextControl{ if (controlCollection == null) return; ...

A generic text clear method for those ASP.NET controls which implement the ITextControl interface:

private void Clear<T>(ControlCollection controlCollection)
    where T : ITextControl
{
    if (controlCollection == null) return;
    controlCollection.Cast<Control>().ToList().ForEach(control =>
    {
        var textCollection = control.Controls.OfType<T>();
        if (textCollection.Count() > 0)
        {
            textCollection.ToList<T>().ForEach(textBox => textBox.Text = string.Empty);
        }
    });
}

Usage: a button click from an ASP.NET page:

protected void btnClear_Click(object sender, EventArgs e)
{
    Clear<TextBox>(Page.Controls);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Comments and Discussions

 
GeneralThis is more generic way to handle controls in a page. PinmemberMukund Thakker26-Sep-11 18:52 
GeneralRe: Thanks Mukund :) PinmemberMohammad A Rahman26-Sep-11 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 22 Sep 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid