Click to See Complete Forum and Search --> : Page_PreInit wire up


yoyosh
February 13th, 2008, 05:37 AM
How to wireup a method for Page_PreInit event in masterpage?

Scott MacMaster
February 25th, 2008, 08:54 PM
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

yoyosh
February 26th, 2008, 02:09 AM
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)

Scott MacMaster
February 26th, 2008, 06:44 AM
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

yoyosh
February 26th, 2008, 01:57 PM
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.

Scott MacMaster
February 26th, 2008, 05:32 PM
Oh, well. Will this work for you?

Default.aspx.vb

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

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

yoyosh
February 27th, 2008, 01:38 AM
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)

technoroj
February 27th, 2008, 01:47 AM
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

Scott MacMaster
February 27th, 2008, 07:57 AM
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

yoyosh
February 27th, 2008, 12:31 PM
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?

Scott MacMaster
February 27th, 2008, 02:45 PM
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)
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
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
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