Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C# 4.0
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.43/5 (7 votes)
22 Sep 2011CPOL 15.4K   2   2
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:


C#
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:


C#
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)


Written By
Software Developer
Australia Australia

Comments and Discussions

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

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

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