Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Microsoft: ASP.NET FAQ

ASP.NET 101

Clear all textboxes/dropdown lists on page
Posted: 19 Nov 06 (Edited 19 Nov 06)

When I started working with .Net I looked and looked to find a way to clear all elements on a page without having to go:

textbox1.text = string.empty
textbox2.text = string.empty
........

and so on, so I wrote this little sub procedure to accomplish this and I'm sharing it with other developers in hopes it might save someone else the time it took me to find a solution for this problem:.

What this does it it runs through the first "container" (a table is a container, the page is a container, etc) and empties the textboxes and selected values of all dropdown lists, then it calls itself until all containers have been cleared. You can leave the ByVal tb As TextBox off if you like, I had it so I could set the focus to a certain textbox when the sub was complete.


CODE

Public Shared Sub EmptyTextBoxes(ByVal parent As Control,ByVal tb As TextBox)
    For Each c As Control In parent.Controls 'LOOP THROUGHN ALL CONTROLS
        If c.GetType() Is GetType(TextBox) Then 'IF ITS A TEXTBOX THEN EMPTY IT
            CType(c, TextBox).Text = String.Empty
            tb.Focus()
        ElseIf c.GetType Is GetType(DropDownList) Then 'ELSE IF ITS A DROPDOWN LIST SET SELECTED VALUE TO -1     
            CType(c, DropDownList).SelectedIndex = 0
        End If
        If c.HasControls Then 'CALL ITSELF (GET ALL OTHER ELEMENTS IN OTHER CONTAINERS)
            EmptyTextBoxes(c)
        End If
    Next
End Sub

I have this Sub in a class file (ReportTemplate.vb) but you can also use it in the code behind file (.aspx.vb). To call it do as follows (this is how I call it in my application from the class file), always use Me as the control (first parameter) to start the Sub on the page level:

CODE

ReportTemplate.EmptyTextBoxes(Me, *textbox name*)

It comes in very hand at the end of Subs or Functions you use to enter data into a database (then you can clear the form at the end of the Sub) or for a Clear button on a form.

On a side note for all those C# developers I also converted this Sub to C#:

CODE

public static void EmptyTextBoxes(Control parent, TextBox tb)
{
 foreach (Control c in parent.Controls) {
   if (c.GetType() == typeof(TextBox)) {
     ((TextBox)(c)).Text = string.Empty;
     tb.Focus();
   } else if (c.GetType == typeof(DropDownList)) {
     ((DropDownList)(c)).SelectedIndex = 0;
   }
   if (c.HasControls) {
     EmptyTextBoxes(c, tb);
   }
 }
}

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close