I'm seeing a rather bizarre behavior when implementing the theme selector control and the setinputcontrolshightlight function from chapter 2. When the page loads and I click on the theme selector dropdownlist, it is automatically posted back (before allowing me to select a value). However, if I tab into the dropdown list, it work fine (including the highlighting) and posts back after I select the value. Has anybody seen this issue? Any ideas of how I can correct it?
Here is my code:
______________________________________________
<%@ Control Language="
VB" AutoEventWireup="false" CodeFile="ThemeSelector.ascx.
vb" Inherits="menichols.UI.Controls.ThemeSelector" %>
<b><asp:Label runat="server" ID="locTheme" Text="Choose Theme: "></asp:Label></b>
<asp:DropDownList runat="server" ID="ddlThemes" AutoPostBack="True" />
------------------------------------------------------------
Imports menichols
Imports menichols.UI
Namespace menichols.UI.Controls
Partial Class ThemeSelector
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Globals.ThemesSelectorID.Length = 0 Then
Globals.ThemesSelectorID = ddlThemes.UniqueID
End If
ddlThemes.DataSource = Helpers.GetThemes()
ddlThemes.DataBind()
ddlThemes.SelectedValue = Me.Page.Theme
End Sub
End Class
End Namespace
---------------------------------------------------------
BasePage
---------------------------------------------------------
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Helpers.SetInputControlsHighlight(Me, "highlight", False)
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
Dim id As String = Globals.ThemesSelectorID
If id.Length > 0 Then
Dim profTheme As ProfileCommon = HttpContext.Current.Profile
' if this is a postback caused by the theme selector's dropdownlist,
' retrieve the selected theme and use it for the current page request
If Me.Request.Form("__EVENTTARGET") = id AndAlso _
Not String.IsNullOrEmpty(Me.Request.Form(id)) Then
Me.Theme = Me.Request.Form(id)
profTheme.Preferences.Theme = Me.Theme
Else
' if not a postback, or a postback caused by controls other then the theme selector,
' set the page's theme with the value found in the user's profile, if present
If Not String.IsNullOrEmpty(profTheme.Preferences.Theme) Then
Me.Theme = profTheme.Preferences.Theme
End If
End If
End If
MyBase.OnPreInit(e)
End Sub
---------------------------------------------------------------
helper.
vb
---------------------------------------------------------------
Public Shared Sub SetInputControlsHighlight(ByVal container As Control, ByVal className As String, ByVal onlyTextBoxes As Boolean)
For Each ctl As Control In container.Controls
If (onlyTextBoxes AndAlso TypeOf (ctl) Is TextBox) OrElse TypeOf (ctl) Is TextBox OrElse TypeOf (ctl) Is DropDownList OrElse _
TypeOf (ctl) Is ListBox OrElse TypeOf (ctl) Is CheckBox OrElse TypeOf (ctl) Is RadioButton OrElse _
TypeOf (ctl) Is RadioButtonList OrElse TypeOf (ctl) Is CheckBoxList Then
Dim wctl As WebControl
wctl = 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
Next
End Sub