How to wireup a method for Page_PreInit event in masterpage?
Printable View
How to wireup a method for Page_PreInit event in masterpage?
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
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)
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
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.
Oh, well. Will this work for you?
Default.aspx.vb
MasterPage.master.vbCode: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
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
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)
Quote:
Originally Posted by yoyosh
Yes
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
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?
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)
MasterPage.master.csCode: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"];
}
}
}
Default.aspx.csCode: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 would be the content page. It and all your content pages originally derived from Page. Simply change them to ParentPage.Code:public partial class _Default : ParentPage // was Page
{
}
Scott