Solved

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

Posted on 2008-10-23
4
669 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yeh thats strange. Ah well you got it working now.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now