Click here to Skip to main content
12,943,742 members (63,564 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 6 Nov 2008

Passing Parameters to Master Page's User Control

, 6 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple use of an interface to pass parameters from a Master page to User Controls.


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:

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).

protected void Page_PreInit(object sender, EventArgs e)
    Page.MasterPageFile = "~/SomeTemplate.Master";

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.

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.


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!!).

public interface ITemplateManupulation
    void SetValueName(int i);
    int GetValueName();

Master Page - Part 1

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.

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;



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!



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.


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


About the Author

Ponzano Paolo
Team Leader Triboo SpA
Italy Italy
I graduated on 2005 in Computer Science in Alessandria (Piedmont),Italy
On 2005 I worked for Dylog s.p.a (Turin) as Windows Application Developer (C#/FW1.1)
On 2006 I worked as .NET Developer for DeltaTre Informatica S.p.A. (Turin) taking part in the realization of IRB.COM and RUGBYWORLDCUP.COM websites
Starting from May 2008 I'm currently the Team Leader and Senior .NET Developer for at Triboo SpA (Milan/Alessandria), currently working full time in an ASP .NET 3.5 WebSite, offering at the same time my experience on other projects (.NET / Architectural one)

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
SabirMohammed15-Sep-10 2:59
memberSabirMohammed15-Sep-10 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 6 Nov 2008
Article Copyright 2008 by Ponzano Paolo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid