Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / XHTML

Passing Parameters to Master Page's User Control

2.77/5 (8 votes)
6 Nov 2008CPOL2 min read 83.6K   533  
Simple use of an interface to pass parameters from a Master page to User Controls.

Image 1

Introduction

Some time ago, I focused on a problem of generating pages starting from templates. A part of the project I'm currently working on needs to have some User Control being loaded dynamically. The User Controls were studied to fit different templates so they had to be as much generic as possible so I had to pass parameters to them. With this article, I'll try to explain a better and easier way to pass parameters to User Controls inside Master Pages.

General idea

The idea I had was to use an ASPX page, to assign it to a MasterPage, and load controls programmatically. The result was I had a quite huge page since it should contain all the checks needed to pass parameters to the ASCX for each template. I made a huge use of:

C#
Control c = Page.LoadControl("~/UserControls/Usercontrol1.ascx");
((ICommonUserControl) c).SetValue1 = "somevalue";

This was useful but made the ASPX page loader quite big... and today, I found another approach I'll explain to you later.... but first, let's have a look at the ASP.NET page lifetime [^].

" PreInit -> Init -> InitComplete -> PreLoad -> Load -> LoadComplete -> PreRender -> SaveStateComplete -> Render -> Unload".

As the Master Page can be set programmatically only on PreInit, that's the point where our setup and load will take part.

Setting the Master Page on the fly

As I told you, the Master Page can be set in the PreInit, and here's the code (I also left the Page_Load empty to show you it's used).

C#
protected void Page_PreInit(object sender, EventArgs e)
{
    Page.MasterPageFile = "~/SomeTemplate.Master";
    ((ITemplateManupulation)Page.Master).SetValueName(7);
}

protected void Page_Load(object sender, EventArgs e)
{
}

How the Web Control reads the data of the template page

As you've seen before, we set the "value" property to 7, and we want it to be read by the User Control.... it's really simple.....take a look.

C#
public partial class DisplayNumberUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int i = ((ITemplateManupulation) this.Page.Master).GetValueName();
        lblNumber.Text = i.ToString();
    }
}

Again, I use the interface to access the property set in the Master Page.

Interface

It's really simple with the use of an interface to access properties without casting them to a specific page type (consider the scenario of 200 different Master Pages!!).

C#
public interface ITemplateManupulation
{
    void SetValueName(int i);
    int GetValueName();
}

Master Page - Part 1

Image 2

As you can see, even here things are really simple... just take a note of DisplayNumberUserControl and the registration in the top of the file if you're not familiar with it.

Master Page - Part 2

And, here's the whole code-behind... really simple in this demo.

C#
public partial class SomeTemplate : System.Web.UI.MasterPage, ITemplateManupulation
{
    public int Value { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    #region Implementation of ITemplateManupulation

    public void SetValueName(int i)
    {
        Value = i;
    }

    public int GetValueName()
    {
        return Value;
    }

    #endregion
}

Result

Here's the result... I know the colours used are not the best, but excuse me, it's a demo application, so just the core counts!

Result.jpg

Summary

I hope you find this article useful. When I needed to develop it, I wasted three days trying to figure out how to generate dynamic slots based on different Master Pages. One another thing: please excuse me if this article is missing in something you may need to know. Right now, those things are familiar to me, but maybe you need further help. Don't hesitate to contact me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)