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

Forums / Developing with Sitefinity / Reading Cookie OnPreInit

Reading Cookie OnPreInit

4 posts, 0 answered
  1. Jess
    Jess avatar
    3 posts
    Registered:
    21 Nov 2007
    12 Jun 2008
    Link to this post
    I have an issue where I need to allow the user to change the website theme on demand and I followed the suggestion in one of the forum threads to create 2 themes and create a class in the App_Code folder to override OnPreInit function and set the theme there.  Then by changing the Inherits attribute of the cmsentrypoint.aspx page from Telerik.Cms.Web.InternalPage to the name of my class, that would use my function.  I decided to set the theme based on a cookie that I create through javascript on one of my pages.

    However, when that code gets executed, I get an error: "HttpCookie" does not contain a definition for "value".

    This is the code that is executing (the red line is where the error is):

    public class SetTheme : Telerik.Cms.Web.InternalPage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            if(Request.Cookies["style"] != null)
            {
                HttpCookie aCookie = Request.Cookies["style"];
                this.Theme = Server.HtmlEncode(aCookie.value);
            }
        }
    }

    I have verified that my cookie exists and contains the desired value.  However, is there something I'm doing wrong when trying to read this cookie?  Is there another way to read the cookie?

    Has anyone tried this before with successful results?

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    13 Jun 2008
    Link to this post
    Hi Jessica,

    We are closing this thread as we have already posted a reply to your inquiry in the support ticket you opened on the same topic. We
    would highly appreciate it if you continue your discussion in the other one.

    Thanks for your cooperation in advance.

    All the best,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Bobby
    Bobby avatar
    1 posts
    Registered:
    30 Nov 2011
    30 Nov 2011
    Link to this post
    This is exactly what I am trying to accomplish as well. Is there a thread which discusses the solution to this matter of dynamically switching the theme and saving the state in a cookie? I cannot seem to find where this discussion is continued...
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    05 Dec 2011
    Link to this post
    Hi Bobby,

    Here is the solution that was provided to Jessica:

    using Telerik.Cms.Web; 
     
    /// <summary> 
    /// Summary description for SetTheme 
    /// </summary> 
    public class SetTheme : InternalPage 
        protected override void OnPreInit(EventArgs e) 
        { 
            base.OnPreInit(e); 
     
            if (Request.Cookies["style"] != null) 
            { 
                HttpCookie cookie = Request.Cookies["style"]; 
                this.Page.Theme = cookie.Value; 
            } 
        } 

    For more information refer to the following MSDN article.

    Hope this helps.

    All the best,
    Pepi
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
4 posts, 0 answered