CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question Page_PreInit wire up

    How to wireup a method for Page_PreInit event in masterpage?

  2. #2
    Join Date
    May 1999
    Posts
    226

    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

  3. #3
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question 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)

  4. #4
    Join Date
    May 1999
    Posts
    226

    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

  5. #5
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    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.

  6. #6
    Join Date
    May 1999
    Posts
    226

    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

  7. #7
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question 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)

  8. #8
    Join Date
    Oct 2007
    Posts
    32

    Re: Page_PreInit wire up

    Quote 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

  9. #9
    Join Date
    May 1999
    Posts
    226

    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

  10. #10
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    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?

  11. #11
    Join Date
    May 1999
    Posts
    226

    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
  •  





Click Here to Expand Forum to Full Width

Featured