Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-23
4
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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