|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWhen 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. BackgroundThis 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 codeFirst 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 The following line of code calls the function and starts the whole process. Note: The declaration 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 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 After checking to make sure the control you are currently looking at, 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 Now all we have to do is put the newly trimmed value of ModificationDo 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||