Click here to Skip to main content
Click here to Skip to main content

Clear All textbox Text On One Click

, 26 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to clear all textbox text on one click


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.


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.

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



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


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

Tell us:

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

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:

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


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. Smile | :)


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


About the Author

Narendra Singh Rathore
Software Developer Pravish Technologies
India India
No Biography provided

Comments and Discussions

Answerthanks PinmemberRam.Cse9-Mar-14 3:26 
General[My vote of 1] Is this a joke? PinmemberAiscrim27-Feb-14 0:33 
GeneralMy vote of 1 Pinmember7045Jeegnesh26-Feb-14 23:49 
better to do in JAVASCRIPT...
GeneralMy vote of 5 PinmemberHumayun Kabir Mamun26-Feb-14 20:23 
GeneralMy vote of 2 PinmemberTorakami26-Feb-14 19:16 
QuestionCleaner More Reusable Version PinmemberAdam R Harris26-Feb-14 8:01 
QuestionHave you considered updating this? PinprofessionalSimon_Whale26-Feb-14 4:57 
GeneralMy Vote of 2 PinmemberBen Bell26-Feb-14 3:22 
QuestionYou just need to use Javascript PinmemberBinh Pham Son25-Feb-14 21:12 
Questionnote Pinmemberlomed25-Feb-14 8:53 
QuestionShorter version PinprofessionalKees van Spelde25-Feb-14 5:51 
SuggestionjQuery offers the simplest solution PinprofessionalBrian A Stephens25-Feb-14 4:09 
GeneralNeed More clarification Pinmemberravikiran geddam25-Feb-14 3:50 
GeneralRe: Need More clarification PinmemberNarendra Singh Rathore25-Feb-14 17:02 
GeneralRe: Need More clarification Pinmemberravikiran geddam7-Mar-14 23:29 

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.150327.1 | Last Updated 26 Feb 2014
Article Copyright 2014 by Narendra Singh Rathore
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid