Link to home
Start Free TrialLog in
Avatar of redmission
redmissionFlag for United States of America

asked on

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

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.
Avatar of M3mph15
M3mph15
Flag of Australia image

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
Avatar of redmission

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of redmission
redmission
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeh thats strange. Ah well you got it working now.