change themes in asp.net 2 with Dropdown

gtjr92

Newcomer
Joined
May 12, 2005
Messages
14
I am trying to set up themes in ASP.Net 2.0 I set up an page load event that grabs the themes by checking the app_themes folder. Added some events for my dropdown list. There are 2 issues i am having
1. When i choose a theme from my dropdown it changes my themes. However when I choose the theme from the dropdown and the page postback the selected item is the first item in the list and not the theme I selected. The theme of the page changes, but the dropdown just defaults back to the first item in list.
I am sure this has something to do with what I have the selected text/value equal too see code below.
2. The other thing I can't figure out is my session is not being saved. After I choose the theme if i click on a link to another page no theme is applied to other pages on the site, view state is enabled for my dropdown list -ddlthemes
See code below
Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 

If Not Page.IsPostBack Then 

'get app themes folders 

Dim themes As String() = IO.Directory.GetDirectories(Request.PhysicalApplicationPath & "App_Themes") 

' 'add themes to dropdown list 
ddlThemes.Items.Clear() 


For Each theme As String In themes 

' add name not path 
ddlThemes.Items.Add(theme.Substring(theme.LastIndexOf( "\") + 1)) 

Next 

End If 
End Sub
'Dropdown list events

Code:
Protected Sub ddlThemes_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles ddlThemes.DataBound 
ddlThemes.SelectedItem.Text = Page.Theme 


End Sub 

Protected Sub ChangeTheme_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlThemes.SelectedIndexChanged 
Session.Add("MyTheme", ddlThemes.SelectedItem.Text) 
Server.Transfer(Request.FilePath) 


End Sub

'basepage class inherited into above page.
Code:
Imports Microsoft.VisualBasic 
Imports System 
Imports System.Data 
Imports System.Configuration 
Imports System.Web 
Imports System.Web.Security 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts 
Imports System.Web.UI.HtmlControls 


Public Class BasePage 
    Inherits System.Web.UI.Page 
    Protected Overrides Sub OnPreInit(ByVal e As EventArgs) 
        MyBase.OnPreInit(e) 
        If Session("MyTheme") Is Nothing Then 
            Session.Add("MyTheme", "Black") 
            Page.Theme = (CType(Session("MyTheme"), String)) 
        Else 
            Page.Theme = (CType(Session("MyTheme"), String)) 
        End If 
    End Sub 
End Class
 
Have you tried it without the ddlThemes_DataBound event handler - that shouldn't be required to maintain the correct selected item.
Also is there a reason why you are doing a Server.Transfer(Request.FilePath) in the ChangeTheme_SelectedIndexChanged - again this shouldn't be required.
 
re

PlausiblyDamp said:
Have you tried it without the ddlThemes_DataBound event handler - that shouldn't be required to maintain the correct selected item.
Also is there a reason why you are doing a Server.Transfer(Request.FilePath) in the ChangeTheme_SelectedIndexChanged - again this shouldn't be required.


I tried removing the Server.Transfer(Request.FilePath)
that does make a change, but not the one i desire. With this change when i click on the drop down list it changes to the wrong theme. IE if i click on a "green" them it changes to the red theme, etc
I will try taking out the drop down events you suggested in the morning
thanks
 
Re

PlausiblyDamp said:
Have you tried it without the ddlThemes_DataBound event handler - that shouldn't be required to maintain the correct selected item.
Ok commented this out and it still works the same so i guess it is not neccessary.
PlausiblyDamp said:
Also is there a reason why you are doing a Server.Transfer(Request.FilePath) in the ChangeTheme_SelectedIndexChanged - again this shouldn't be required
when i comment out the server.transfer i click on the drop down list it changes to the wrong theme. IE if i click on a "green" them it changes to the red theme, etc

In all scenarios the theme is never applied to any page except the page that has the ddl.

Other ideas??
Thanks
 
The problem is that Server.Transfer doesn't preserve the form's data unless you specify you want it to (add an extra boolean parameter). However doing this causes an infinite loop...

Bit of a dirty hack but this works for me and should get you onto the right track.
Visual Basic:
    Protected Sub ChangeTheme_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlThemes.SelectedIndexChanged
        If Session("test") Is Nothing Then
            Session("Test") = True
            Session.Add("MyTheme", ddlThemes.SelectedItem.Text)
            Server.Transfer(Request.FilePath, True)

        Else
            Session("test") = Nothing
        End If
    End Sub
 
half way there

changing the selected index changed event solved the dropdown list problem.
However the theme is still only being applied to one page, and not site wide.
If I manually change the them in the webconfig file the theme is applied properly so i must be missing something else in my code.
 
got it

Figured out my theme problem, i was not inheriting the basepage class to the rest of my pages DUH!!
Thanks for your help!
 
Back
Top