|
-
February 13th, 2008, 06:37 AM
#1
Page_PreInit wire up
How to wireup a method for Page_PreInit event in masterpage?
-
February 25th, 2008, 09:54 PM
#2
Re: Page_PreInit wire up
The content page initiates everything. If you want to handle preinit it would have to with the content page.
I don't know why you would need to handle preinit in the master page. What are you trying to do?
Scott
-
February 26th, 2008, 03:09 AM
#3
Re: Page_PreInit wire up
While having a hundred content pages, this task may be quite exhausting. All I`m trying to do is to allow user to change application`s theme via UI (eg. dropdown)
-
February 26th, 2008, 07:44 AM
#4
Re: Page_PreInit wire up
Hmm, I've never done that. My first approach would have that drop down on the master page. Then, code the selected index event handler to select a different css file. Am I correct in assuming you are changing the theme by using different css files?
Scott
-
February 26th, 2008, 02:57 PM
#5
Re: Page_PreInit wire up
Having dropdown on masterpage is exactly what I wanted to do.
I`m affraid you are wrong with your assumptions. CSS means styles, not themes. Themes are placed inside a special folder (App_Themes) and they contain *.skin files. They may also contain css files, but let`s assume for the sake of that problem that they don`t.
Suppose that we have 2 themes, and each of them contain one *.skin file. I want to switch between them from masterpage.
-
February 26th, 2008, 06:32 PM
#6
Re: Page_PreInit wire up
Oh, well. Will this work for you?
Default.aspx.vb
Code:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
If Session("Theme") Is Nothing Then
Page.Theme = "Default Theme"
Else
Page.Theme = Session("Theme")
End If
End Sub
End Class
MasterPage.master.vb
Code:
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub lstThemes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstThemes.SelectedIndexChanged
Session("Theme") = lstThemes.SelectedValue
Server.Transfer(Request.CurrentExecutionFilePath)
End Sub
End Class
Scott
-
February 27th, 2008, 02:38 AM
#7
Re: Page_PreInit wire up
I don`t know vb at all, but as I guess your solution includes writing some custom code in EVERY content page, what I`m trying to avoid. Am I correct? I would like to write this custom code only one time (this means that it should be written in .master only)
-
February 27th, 2008, 02:47 AM
#8
Re: Page_PreInit wire up
 Originally Posted by yoyosh
I don`t know vb at all, but as I guess your solution includes writing some custom code in EVERY content page, what I`m trying to avoid. Am I correct? I would like to write this custom code only one time (this means that it should be written in .master only)
Yes
-
February 27th, 2008, 08:57 AM
#9
Re: Page_PreInit wire up
Well, I was just asking if Server.Transfer() will work for you. You seem to have a lot of pages. It may interfere if any of your pages use query strings. If they do you have to add code to get the query string and append the theme to it.
Ignore the VB. What I have translates straight to C#. Just add a handler for selected index changed changed and preinit.
As for repeating the code in the content page. You won't have to do that. Just subclass the content page.
Scott
-
February 27th, 2008, 01:31 PM
#10
Re: Page_PreInit wire up
Indeed we may assume that I have many pages. But I don`t understand your solution. It seems to me, that I would have to implement Pre_Init method in every page in my application. Please correct me. You also wrote that I should change just a subclass of content page. What subclass?
-
February 27th, 2008, 03:45 PM
#11
Re: Page_PreInit wire up
By sub class I mean derive an class for System.Web.UI.Page that handles PreInit then have all your content pages derive from it.
ParentPage.cs (put this under App_Code)
Code:
using System;
using System.Web.UI;
public class ParentPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
// Allow The Base Class To Intialize First
base.OnInit(e);
if (Session["Theme"] == null )
{
Page.Theme = "Default Theme";
}
else
{
Page.Theme = (string)Session["Theme"];
}
}
}
MasterPage.master.cs
Code:
using System;
using System.Web.UI;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Theme"] = lstThemes.SelectedValue;
Server.Transfer(Request.CurrentExecutionFilePath);
}
}
Default.aspx.cs
Code:
public partial class _Default : ParentPage // was Page
{
}
Default.aspx would be the content page. It and all your content pages originally derived from Page. Simply change them to ParentPage.
Scott
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|