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

Tagged as

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

, 22 Sep 2011 CPOL
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


Comments and Discussions

 
GeneralThis is more generic way to handle controls in a page. PinmemberMukund Thakker26-Sep-11 19:52 
GeneralRe: Thanks Mukund :) PinmemberMohammad A Rahman26-Sep-11 19: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 | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Sep 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid