Click here to Skip to main content
Click here to Skip to main content
Go to top

Quick and Dirty Textbox Trimming

, 20 Jul 2005
Rate this:
Please Sign up or sign in to vote.
An article on easily trimming every textbox on an ASP.NET webpage.

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

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

Share

About the Author

Craigpt
Web Developer
Canada Canada
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.

Comments and Discussions

 
GeneralSuggestion to Sanitize Strings PinmemberVasudevan Deepak Kumar21-Jul-05 7:28 
GeneralA suggestion PinmemberLuke Foust20-Jul-05 7:26 
GeneralRe: A suggestion PinmemberVasudevan Deepak Kumar21-Jul-05 7:27 
GeneralRe: A suggestion PinmemberCraigpt22-Jul-05 5:37 

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 | Mobile
Web03 | 2.8.140905.1 | Last Updated 20 Jul 2005
Article Copyright 2005 by Craigpt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid