views:

98

answers:

1

I have quite simple site structure with one mastepage and a bunch of pages. The mastepage is quite advanced though and I need from the page be able to control certain aspects of the Masterpage.

I want to be able to enter these directive in the aspx file to not clutter the code behind files.

My idea was to create different "directive" user controls such as SeoDirective:

using System;

public partial class includes_SeoDirective : System.Web.UI.UserControl
{

    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; } 

}

I include this directive in those pages that need to override the default mastepage settings.

<offerta:SeoDirective runat="server" Title="About Us" MetaDescription="Helloworld"/>

In my masterpage I look if there's any directives:

includes_SeoDirective seo = (includes_SeoDirective) ContentPlaceHolder1.Controls.Children().FirstOrDefault(e => e is includes_SeoDirective);

(Children() is an extension so I can work with Linq on a ControlCollection)

Now to my question: I'm not to happy about this solution might be a bit bloated?

I'm looking for alternative solutions where I can created these tags in the aspx file.

I've looked at the trick where I extend the Page, but that requiries we to modify the VS configs for the project to compile, so I dropped that solutions.

+1  A: 

As far as I am aware, there isn't a standard way of doing this. I have done this same thing in the past in much the same way you have, except I used an interface on the pages that I needed the master page to look for, which defined a method it could call to do specific logic with the master page.

You might be able to use this same paradigm:

ISpecialPage.cs:

public interface ISpecialPage
{
    string Title { get; set; }

    string MetaDescription { get; set; }

    string MetaKeywords { get; set; } 
}

MyPage.aspx:

public partial class MyPage : System.Web.UI.Page, ISpecialPage
{
    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        this.Title = "Some title";
        this.MetaDescription  = "Some description";
        this.MetaKeywords = "Some keywords";
    }
}

MasterPage.master:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Context.Handler is ISpecialPage)
        {
            ISpecialPage specialPage = (ISpecialPage)this.Context.Handler;
            // Logic to read the properties from the ISpecialPage and apply them to the MasterPage here
        }
    }
}

This way you can handle all MasterPage logic in the master page code behind file, and simply use the interface on pages you need to provide certain information.

Hope this helps you!

Codesleuth
Cool, but I wanted to set Title etc. in the aspx file?
Niels Bosma
if you mean the <head><title> element, the value of this is automatically taken from the page that is running - the masterpage title is discarded. You can either specify this in the <%@ Page Title="title" %> markup for the page, or set it in code with this.Title = "title"; in, for example, the Page_Load method. Does this work for you?
Codesleuth