Modifying master page properties at run time

by Codewiz51 July 01, 2009 20:50

I host codewiz51.com web site on webhost4life.com.  I don't want to spend $40 on an SSL certificate, so I use the free shared SSL certificate.  One of the problems is that the web address is different between the non-SSL and SSL access URLs and directories.  For instance, you are on http://www.codewiz51.com/blog, but the SSL access is https://siteabc.mysite4now.net/starwars/yoda (this address will give you an error.)

In order to get around this problem, I've modified the event handlers for the default.aspx page on my main web site to show "(Secure)" appended to the html title element when using SSL.

Here's the relevant section of yoda.master.  Noticed that I do not include a title tag in the head.  Each content page supplies the title as a content string for the ContentPlaceHolder "head".


<head id="Head1" runat="server">
    ...
    <asp:ContentPlaceHolder ID="head" runat="server" />
</head>

Here's the code I've added to default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Url.ToString().Contains("https:"))
        {
            ContentPlaceHolder mpHeader = (ContentPlaceHolder)Master.FindControl("head");
            HtmlTitle title = new HtmlTitle();
            title.Text = @"Home (Secure)";
            mpHeader.Controls.Add(title);
        }
        else
        {
            ContentPlaceHolder mpHeader = (ContentPlaceHolder)Master.FindControl("head");
            HtmlTitle title = new HtmlTitle();
            title.Text = @"Home";
            mpHeader.Controls.Add(title);
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.Url.ToString().Contains("https:"))
            this.MasterPageFile = @"~/starwars/yoda.master";
        else
            this.MasterPageFile = @"~/yoda.master";
    }

The first routine, Page_Load, simply adds the correct title to the html for the page.

The second routine, Page_PreInit is more important.  It modifies the path to the master page so that ASP.Net can find the file when using SSL.

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant




Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008