1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Event Not Firing

Event Not Firing

4 posts, 0 answered
  1. Allen
    Allen avatar
    71 posts
    Registered:
    04 Aug 2007
    12 Sep 2007
    Link to this post
    I would like to add some code to my master page to allow the user to change the site's them (specifically to allow a printer friendly page).  I already had a master page which had no code behind or .vb file for it so I went directly to the physical file to add the code behind.  However, the event is not firing.  Do I need to reupload the master page or is this not possible?  Furthermore, since I am altering the theme via code but have themes assigned in the Sitefiity UI, will there be an issues with code fired in the preinit being overriden by Sitefinity events?
  2. Bob
    Bob avatar
    330 posts
    Registered:
    30 Dec 2016
    12 Sep 2007
    Link to this post
    Hello Allen,

    You cannot subscribe for Page.PreInit event in a master page since the master page instances are created after the PreInit event is fired and themes are initialized. You can take a look at the implementation of System.Web.UI.Page.PerformPreInit method:

    private void PerformPreInit()
    {
    this .OnPreInit(EventArgs.Empty);
    this .InitializeThemes();
    this .ApplyMasterPage();
    this ._preInitWorkComplete = true ;
    }



    To set themes programmatically, please follow these steps:
    1. Create a new class in App_Code folder that inherits from Telerik.Cms.Web.InternalPage and override the OnPreInit method. Enter your logic in this method.

    2. public class CustomPage : Telerik.Cms.Web.InternalPage
      {
      protected override void OnPreInit(EventArgs e)
      {
      base .OnPreInit(e);
      this .Theme = "MyTheme" ;
      }
      }


    3. Replace the declaration of InternalPage class with the name of your newly created class in the following file ~/Sitefinity/cmsentrypoint.aspx The file should look like this:

    4. < %@ Page Language = "C#" MasterPageFile = "~/App_Master/Dummy.master" Inherits = " CustomPage " % >


      Sorry for posting the code in C#. Let me know if this is a problem for you and I will translate the code.

      By the way, this seems to be a common problem so we are considering allowing alternate themes for pages through Sitefinity’s user interface. This feature will probably be available in version 3.2.

      Greetings,
      Author nickname
      the Telerik team

      Instantly find answers to your questions at the new Telerik Support Center
  3. Allen
    Allen avatar
    71 posts
    Registered:
    04 Aug 2007
    14 Sep 2007
    Link to this post
    No worrys about the C#, I'm bi-lingual.  :)

    The solution you offer does get me down the road a bit, but I can't seem to complete the process.  I might should explain what I am trying to do as it may not be the best approach.  I appreciate your feedback.

    Our application must support visually impaired individuals so I want to create a variety of themes that can accommodate that.  Additionally, I would like a "vanilla", text theme for individuals that may have dial-up or that do not like the graphic intense design.

    My goal would be to have http://somepage.aspx?mode=Text or http://somepage.aspx/text be the url.  Additionally, I really want to have only one set of pages for the designers to maintain (and not a set of pages for each theme).  However, I cannot find how to capture the CMS url.  Now that I have your code sample, I can swith the theme, but have no mechanism to catch which theme to switch to.  For example, I have some link buttons which offer the user options for themes.  My goal would be to have the user click the link and then have code to switch the them in the postback.  Is this possible?  If so, is this a reasonable approach in Sitefinity?

    Thanks in advance.
  4. Bob
    Bob avatar
    330 posts
    Registered:
    30 Dec 2016
    14 Sep 2007
    Link to this post
    Hello Allen,

    This is absolutely possible. Below are some examples of ways to retrieve information form the current request:

    using System;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.Security;  
    using System.Web.Profile;  
    using Telerik.Cms;  
    using Telerik.Cms.Web;  
     
    public class CustomPage : InternalPage  
    {  
        protected override void OnPreInit(EventArgs e)  
        {  
            base.OnPreInit(e);  
              
            //Get page ID from base class;  
            Guid pageId1 = base.CmsPage.ID;  
            //Get page ID from CmsUrlContext  
            Guid pageId2 = CmsUrlContext.Current.PageID;  
     
            //Get the default URL for the current page  
            string url1 = base.CmsPage.StaticUrl;  
            //Get the reqested URL  
            string url2 = base.Request.RawUrl;  
     
            //Get authenticated user for the current request  
            MembershipUser user = Membership.GetUser();  
              
            //Ways to get the user name for the current request if authenitcated  
            string userNmae;  
            //From MembershipUser class  
            if (user != null)  
                userNmae = user.UserName;  
            //From current page  
            if (base.User.Identity != null)  
                userName = base.User.Identity.Name;  
     
            //Get profile for user.   
            //Note, profiles can be created for anonymous users as well.  
            ProfileBase profile = ProfileBase.Create(userName);  
     
            //Get theme for the current user  
            //Note, Theme property should be declared in web.config  
            string theme = profile.GetPropertyValue("Theme"as string;  
        }  
    }  
     


    Kind regards,
    Bob
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Register for webinar
4 posts, 0 answered