'add onfocus and onblur to controls so active control has a different appearance
Utilities.Helpers.SetInputControlsHighlight(Page, "highlight", True)
MyBase.OnLoad(e)
End Sub
For Each ctl As Control In container.Controls
If ((OnlyTextBoxes AndAlso TypeOf ctl Is TextBox)
Or (Not OnlyTextBoxes
AndAlso (TypeOf ctl Is TextBox
Or TypeOf ctl Is DropDownList
Or TypeOf ctl Is ListBox
Or TypeOf ctl Is CheckBox
Or TypeOf ctl Is RadioButton
Or TypeOf ctl Is RadioButtonList
Or TypeOf ctl Is CheckBoxList))) Then
Dim wctl As WebControl = CType(ctl, WebControl)
wctl.Attributes.Add("onfocus", String.Format("this.className = '{0}';", ClassName))
wctl.Attributes.Add("onblur", "this.className = '';")
Else
If (ctl.Controls.Count > 0) Then
SetInputControlsHighlight(ctl, ClassName, OnlyTextBoxes)
End If
End If
Next
End Sub
The function is pretty simple. It's setup to take the root container as a parameter and it will apply the specified class (e.g. "highlight") to the controls in that container. There is also a parameter to specify if you want the style applyed to all input controls on the form or specifically just TextBox controls.
In my case when the user clicks on an input control on the form it shows the control with a different color so it's obvious that it has the focus, then when the focus is lost it goes back to normal, etc. It can easily be modified to apply to other situations.
The original idea for this code was based the book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso. It's one of the best books I've used throughout my career. It's published by WROX, and all of their books are usually very good (in my opinion), but this one is different than most because rather than a typical reference book, it's actually a step by step solution for building an ASP.Net 2.0 website from the ground up, from listing requirements, identifying problems, designing the database, architecting the solution, implementation, all the way to deployment, etc.
0 comments: