Solved

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

Posted on 2008-10-23
4
678 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Class Library Dynamics For Connectstring Information 2 28
designing in object programming 12 78
rebind a grid after user clicks on node in treeview 1 35
ASP.NET MVC 2 31
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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