Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changes themes with dropdown list in .net 2.0

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
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.
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 
'Dropdown list events
Code:
 'dropdown events
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
Code:
'BasePage Code

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", "PSU") 
Page.Theme = (

CType(Session("MyTheme"), String)) 

Else 
Page.Theme = (

CType(Session("MyTheme"), String)) 

End If 

End Sub 
End Class
 
gtjr92: Couple of notes.

Regarding your ddl not showing up as the selected Item on postback the cause is generally that the selectedItem is somehow not preserved or propertly assigned to the ddl on postback.

Let's do a walkthrough on the code, and you can post back and help me along with code I am not used to. Looking at the Page Load event:
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
Ok, I haven't tested this but it appears that on the initial Page Load event you are populating your ddl (id="ddlThemes") with the names of files within a specified directory.

First thing. Why are you clearing the ddl on the initial Page Load? Don't see why that is necessary; but you may be right here; its just something I haven't seen done.

Ok, lets assume that the initial binding goes well and not problems up to this point. Looking at the second bit of code:
Code:
...
ddlThemes.SelectedItem.Text = Page.Theme
...
You might have to help me on this one; it appears to be an event triggered whenever the ddl is bound to data, so would fire on the initial page load. This is a bit confusing to me as I see nowhere where "Theme" is defined.

Going on to the third bit of code which is triggered by an actual change of ddlThemes (which I assume is set to AutoPostBack="true") you show:
Code:
...
Session.Add("MyTheme", ddlThemes.SelectedItem.Text)
Server.Transfer(Request.FilePath)
...
Where you are showing the population of Session with the ddl Text. Another way of doing this would be, e.g.,

Session("MyTheme") = ddlThemes.SelectedItem.Text

Not sure whether there would be a preferene one way or the other, they are probably equivalent.

So far no problems I can see. The next bit of code is common here, I see it a lot, but have never used it myself (I use primarily the Redirect and PostBack) is the statement:

Server.Transfer(Request.FilePath)

It would seem, on first glance, that this might conflict with the AutoPostBack="true" which fires a postback of the page to the server; here you are posting back the Form? Again, not entirely familiar with this but have seen it discussed.

This would be one spot to take a look at. If you have AutoPostBack="true" I would guess (can't be certain here without testing) that this is problem spot. Try eliminating this Server.Transfer and see what happens.

The third part of your code seems find since you are only repopulating your Session when it cleared by the garbage collector, etc. Session usually hangs in there pretty good but if you have a lot of memory leakage (unclosed objects, etc, somewhat compliated affair) you can loose Session earlier than normal.

My guess, and post back and let me know how it goes, is to take out the Server.Transfer and try a straightaway post back and see what happens. Your ddl should have its ViewState turned on by default. Let me know what you find.
 
Well 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. If i click on a link within my site my theme is applied, but not the right theme. Not even the one that i changed it too. Not sure. All i know is i do not believe the Server.Transfer(Request.FilePath)
is the problem here.

To answer your questions
Theme is part of the .net 2 framework and does not need to be defined. you can set them in the webconfig if i was not setting them programatically. Since i am then i did not set it up in webconfig file.

Why are you clearing the ddl on the initial Page Load?
if i don't then the ddl keep getting re populated with same data so will have duplicate items in the ddl.

ANybody have any ideas on my initial problem listed in my first post?
Thanks
 
Ok i just discover this as well, when i commented out the Server.Transfer(Request.FilePath)
If i click on one of my internal links to my site it changed the theme to the very firt theme that was loaded when i made this change that them is applied, however it stays applied to all my pages even after i choose a different theme. Even now when I uncommented that request.file path the them is applied to all my pages except the default page that the ddl is on when i change that then the theme for that change.
This may be too much info.
Anybody?
 
gtjr92: I am going to run some laptop test here on your code and try and reproduce this behavior. I'm still a little confused as to why your ddl would append new bindings on post back when your binding code is in the location of "Page Load, Not IsPostBac", this should only bind once, and therefore I'm not sure why it would be necc. to Clear.

Perhaps this will work out better if the binding is extracted from the Page Load and placed within its own Sub routine. I'll do some testing on the laptop this morning gtjr92 and see if I can't run into your problem.

Perhaps someone will drop by as well and give their 2 cents. Whatever it is, no worries, we'll get it figured out (I can't imagine a problem of this nature to be anything more than a somewhat simple oversight (famous last words!)).
 
gtjr92: I written up you page and testing; I cannot get past the following:

ddlThemes.SelectedItem.Text = Page.Theme

..not recognizing Page.Theme; Error message:

'Theme' is not a member of 'System.Web.UI.Page'.

Am I missing a Page Directive, or something else?
 
here's the full code for that basepage class.
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

as far as the ddl I had that problem before with the ddl repopulating duplicate data and doing that fixed it. I am in a habint of doing that so i didn't bother testing it on this one i just used that code out of habit.
thanks for the help
 
gtjr92: I'm outside of Visual Studio working from Notepad; couldn't get the code to behave like I wanted.

The problem may perhaps be in your Page Init code; it seems that once a ddl option is selected that you are loosing this in the recompiling of the new page after hitting the server on postback. As we mentioned earlier ViewState shoudl be on as that is the ddl's default I believe.

This really should be a simple matter. What I do in a situation like this, generally, is to set a value (ViewState, Public Variable, hidden textbox to the SelectedIndex of the ddl on its changed index sub. Then I call another sub to apply this change to a new SQL hit of the tables, say, for a Grid change, etc.

Perhaps you could add an "Is PostBack" statement that sets the SelectedIndex of the ddl to the captured value above.

If you do not get a response form others by the time this thread sinks 10 or so re-post it in a condensed matter and lets see if someone elese can drop in and help you find out the subtlty of the problem. It has to be a very simple matter as it seems you are only loosing your position in the ddl, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top