Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET - Add Attributes To All Text Boxes On Page

Posted on 2008-10-23
4
Medium Priority
?
694 Views
Last Modified: 2008-11-03
I have this code, and it works:
With Me
            .txtFullName.Attributes.Add("onfocus", "this.style.backgroundColor='#F4EDBD';")
            .txtFullName.Attributes.Add("onblur", "this.style.backgroundColor='#FFFFFF';")
        End With

I want to do this with ALL text boxes on the page though.  How can I loop through all the controls and do this just for the text boxes so I want have to manually type all the ID names for the text boxes?  I have my code in the Page_Load event.
0
Comment
Question by:redmission
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22792982
Dim c As Object    
        For Each c In Page.Controls
              'TextBox
             If (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) Then
                  Dim tb As TextBox = DirectCast(c, TextBox)
                  tb.Attributes.Add("onfocus", "this.style.backgroundColor='#F4EDBD';")
                  tb.Attributes.Add("onblur", "this.style.backgroundColor='#FFFFFF';")
               End If
         Next
0
 

Author Comment

by:redmission
ID: 22832755
I put this code in the Page Load event.  It's the only code in that event.  And it doesn't work on the page.  I tested in Firefox, IE, Safari...Javascript enabled in all browsers.
0
 

Accepted Solution

by:
redmission earned 0 total points
ID: 22834451
I looped through all the controls in the page and sent them to this procedure, and this happens to work!  I'm not sure why yours wouldn't work though.

Public Sub ColorTextBoxes(ByVal _c As Control)
        Dim c As Control, t As TextBox
        For Each c In _c.Controls
            If (TypeOf c Is TextBox) Then
                t = CType(c, TextBox)
                t.Attributes.Add("onfocus", "this.style.backgroundColor='#F4EDBD';")
                t.Attributes.Add("onblur", "this.style.backgroundColor='#FFFFFF';")
            Else
                ColorTextBoxes(c)
            End If
        Next
    End Sub
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 22837292
Yeh thats strange. Ah well you got it working now.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question