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.
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
This is the function that gets so lovingly called by our ASP.NET page.
Public Sub TrimTextBoxes(ByVal ControlSet As Control)
For Each Ctrl As Control In ControlSet.Controls
If Not Ctrl.UniqueID Is Nothing Then
If Ctrl.UniqueID.Substring(0, 3) = "txt" Then
Dim tempText As System.Web.UI.WebControls.TextBox
tempText = Ctrl
tempText.Text = tempText.Text.Trim
Ctrl = tempText
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!
Do you use a different naming convention? Are there different controls you want to deal with? Do you want to do something other than
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.clear. This function makes a good multi-use piece of code. :)