Click here to Skip to main content
15,881,027 members
Articles / Web Development / ASP.NET
Tip/Trick

Clear All textbox Text On One Click

Rate me:
Please Sign up or sign in to vote.
4.09/5 (15 votes)
26 Feb 2014CPOL1 min read 59.1K   9   16
How to clear all textbox text on one click

Introduction

When we are making a registration form or a form having many textboxes, i.e.,[10,20-50 textboxes] in a single page and we have to clear all the textboxes at once after submission, what we do is write dozens of lines for every textbox out there in form or page.

If you have one or two textboxes, then it is ok.

Just use textbox-id.Text=""; and it will clear the textbox text.

But when we have many textboxes in a single page of form, then we have to use something better to do that and here this code comes in.

So just read, understand and apply it.

Background

You need a little bit of information about how to create a control in ASP.NET from code-behind.

Using the Code

Here, I have taken a div in which I have placed my textbox and other controls in it.

HTML
 <div id="feedback" runat="server">

        <h3>Feedback</h3>

                 Name:

         <asp:TextBox ID="name" runat="server" ></asp:TextBox>

                  Email: 

<asp:TextBox ID="email" runat="server" ></asp:TextBox>

Tell us:

<asp:TextBox ID="text" runat="server" 
CssClass="feedbacktextbox" TextMode="MultiLine" >
</asp:TextBox>
<asp:Button ID="b1" CssClass="feedbackbtn" 
runat="server" Text="Submit" OnClick="b1_Click" />      
</div>  

So, now on code-behind, when button is clicked, it runs the code in which a foreach-loop loops through our division and finds the TextBox control, wherein code below b is Control which will loop through our division.

c is a textbox control we declared.

And what will happen after that is below:

C#
protected void b1_Click(object sender, EventArgs e)
{ 
 foreach ( Control b in feedback.Controls ) //feedback is division

            {
                TextBox c;
                if (b is TextBox)
                {
                    c = b as TextBox; 
                    if (c != null) 
                    {
                        c.Text = String.Empty;

                    // or we can use

                     c.Text="";
                    }
                }
            } 
} 

Points of Interest

Using this will save us from writing dozens of lines to clear TextBox text.

So, guys, use it or leave it. It's your choice. :)

License

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


Written By
Software Developer Pravish Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionC# Pin
Member 137700899-Apr-18 3:46
Member 137700899-Apr-18 3:46 
Answerthanks Pin
Ram.Cse9-Mar-14 2:26
Ram.Cse9-Mar-14 2:26 
General[My vote of 1] Is this a joke? Pin
Aiscrim26-Feb-14 23:33
Aiscrim26-Feb-14 23:33 
GeneralMy vote of 1 Pin
7045Jeegnesh26-Feb-14 22:49
7045Jeegnesh26-Feb-14 22:49 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun26-Feb-14 19:23
Humayun Kabir Mamun26-Feb-14 19:23 
GeneralMy vote of 2 Pin
Torakami26-Feb-14 18:16
Torakami26-Feb-14 18:16 
QuestionCleaner More Reusable Version Pin
Adam R Harris26-Feb-14 7:01
Adam R Harris26-Feb-14 7:01 
I answered the exact same question except in VB just a few months ago here.
Here is the C# version of it, it uses recursion and allows you to just pass in the container and it will clear all the textboxes inside and containers inside it.

C#
public void ClearTextboxes(ControlCollection controls)
{
    foreach (Control ctrl in controls){
        if  (ctrl is Textbox){
            ctrl.Text = String.Empty;
        } else if (ctrl.HasControls) {
            ClearTextboxes(ctrl);
        }
    }
}


Call it like:
C#
ClearTextboxes(MyContainer.Controls);

Don't comment your code - it was hard to write, it should be hard to read!

QuestionHave you considered updating this? Pin
Simon_Whale26-Feb-14 3:57
Simon_Whale26-Feb-14 3:57 
GeneralMy Vote of 2 Pin
Ben Bell26-Feb-14 2:22
Ben Bell26-Feb-14 2:22 
QuestionYou just need to use Javascript Pin
Binh Pham Son25-Feb-14 20:12
Binh Pham Son25-Feb-14 20:12 
Questionnote Pin
lomed25-Feb-14 7:53
lomed25-Feb-14 7:53 
QuestionShorter version Pin
Kees van Spelde25-Feb-14 4:51
professionalKees van Spelde25-Feb-14 4:51 
SuggestionjQuery offers the simplest solution PinPopular
Brian A Stephens25-Feb-14 3:09
professionalBrian A Stephens25-Feb-14 3:09 
GeneralNeed More clarification Pin
ravikiran geddam25-Feb-14 2:50
ravikiran geddam25-Feb-14 2:50 
GeneralRe: Need More clarification Pin
Narendra Singh Rathore25-Feb-14 16:02
professionalNarendra Singh Rathore25-Feb-14 16:02 
GeneralRe: Need More clarification Pin
ravikiran geddam7-Mar-14 22:29
ravikiran geddam7-Mar-14 22:29 

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.