Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
22 Sep 2011 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here