Click here to Skip to main content
6,595,854 members and growing! (17,483 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Quick and Dirty Textbox Trimming

By Craigpt

An article on easily trimming every textbox on an ASP.NET webpage.
VB, Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:20 Jul 2005
Views:26,467
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 1.74 Rating: 1.82 out of 5
5 votes, 55.6%
1
1 vote, 11.1%
2

3

4
3 votes, 33.3%
5

Introduction

When dealing with textboxes in ASP.NET pages, it is a good idea to trim the data, since spaces are so hard to see. Once a page gets to a decent size, trimming every textbox can easily become a tedious task. The purpose of this sub is to quickly trim every textbox on your webpage, without having to trim them all one by one.

This subroutine can be placed in a Module and therefore can be called by any webpage in your project.

Background

This subroutine depends completely on iteration. A basic understanding of iteration is helpful in understanding how this works.

This subroutine also works best if you've used naming conventions for your controls. Each one of my textboxes/areas has an ID starting with "txt". This becomes imperative in knowing which controls should/shouldn't and can/can't be trimmed.

Using the code

First thing you want to do, is call the function, and pass in the form you're working on. Control sets in VS.NET have a spiffy little function called FindControl that returns the control with the ID that you've passed in.

The following line of code calls the function and starts the whole process. Note: The declaration Me does not indicate the form itself. The form is a control inside of Me.

TrimTextBoxes(Me.FindControl("Form1"))

This is the function that gets so lovingly called by our ASP.NET page.

Public Sub TrimTextBoxes(ByVal ControlSet As Control)
    'This function takes in the form(or set of controls) 

    'you are working on, and iteratively loops through 

    'every control on the form and trims any control based 

    'on a naming convention.

    
    For Each Ctrl As Control In ControlSet.Controls
        'Iterative Loop

        TrimTextBoxes(Ctrl)
        'Makes sure the control has a name

        If Not Ctrl.UniqueID Is Nothing Then
            'If the control name matches my naming convention

            If Ctrl.UniqueID.Substring(0, 3) = "txt" Then
                'Create a TEMPORARY text box

                Dim tempText As System.Web.UI.WebControls.TextBox
                'Set temp to be the control we're looking at

                tempText = Ctrl
                'Since tempText is guaranteed to be a textbox

                'we can trim tempText.text

                tempText.Text = tempText.Text.Trim
                'Put the value back into our control

                Ctrl = tempText
            End If
        End If
    Next
End Sub

Since each control is also a control set, the first thing we do is setup the For loop to go through each control that exists within each control.

Also, since the number of controls in each control set is unknown, and we just want to keep looping down through the controls' controls, this is where the iteration comes in. The Sub then calls itself, which will loop all the way down to the very last control in the first control set of the first control set, etc...

After checking to make sure the control you are currently looking at, Ctrl, has a name (no reason it shouldn't, just a safety precaution), we check to see if the name starts with "txt". If it does, then, due to our naming convention, we know it's a textbox.

Unfortunately for us, we now know that this control is a textbox, VS.NET doesn't. So what we do is create a blank textbox control called tempText in this example, and then put the value of Ctrl inside the temporary textbox. Now we can trim tempText, because VS.NET knows it's a textbox.

Now all we have to do is put the newly trimmed value of tempText back into Ctrl and we're done!

Modification

Do you use a different naming convention? Are there different controls you want to deal with? Do you want to do something other than Trim?

Fortunately, this is quite easy to change to do what you want. Say you want to clear all the textboxes on the page instead. Just change tempText.trim to tempText.clear. This function makes a good multi-use piece of code. :)

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

About the Author

Craigpt


Member
I'm currently 21 and live in Sherwood Park, Alberta. I've just finished up my diploma in Computer Systems Tech at NAIT in December of 2005.
Occupation: Web Developer
Location: Canada Canada

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralSuggestion to Sanitize Strings PinmemberVasudevan Deepak Kumar8:28 21 Jul '05  
GeneralA suggestion PinmemberLuke Foust8:26 20 Jul '05  
GeneralRe: A suggestion PinmemberVasudevan Deepak Kumar8:27 21 Jul '05  
GeneralRe: A suggestion PinmemberCraigpt6:37 22 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jul 2005
Editor: Rinish Biju
Copyright 2005 by Craigpt
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project