Click here to Skip to main content
Click here to Skip to main content

Tagged as

Calling Master Functions Through A Contract

, 29 Apr 2011
Rate this:
Please Sign up or sign in to vote.
When to call master functions through a contract (abstract class, Interface etc.,)
Calling master functions and properties are a decade old question and the usual way is to define the master type in the content page i.e <%@ MasterType VirtualPath="~/Site.master" %> . This works well however it creates a strong dependency with one master type. How about changing the master dynamically from the content page. The situation gets worse. In that time, the masters have to be accessed to a common contract. So that whenever the master changes and sticks to the contract, the agreements never fails. The contract could be a super class, abstract class or an interface.
 
This sample code is about use of an interface. Have a simple interface in the App_code folder:
 
public interface ICommonMasterFunctions
{
    void setPageNumber(string number);
}
 
Have a label in the master page:
 
<div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
 
Implement the interface to all the masters required.
 
public partial class SiteMaster : System.Web.UI.MasterPage,ICommonMasterFunctions
{
    public void setPageNumber(string number)
    {
        Label1.Text = "You are on the page " + number.ToString();
    }
}
 
Now you can pass the page number from the content page:
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ICommonMasterFunctions master = (ICommonMasterFunctions)this.Master;
        master.setPageNumber("5");
    }
}
 
As all the required masters agreed on this contract, changing the master doesn't need to change the code. This gives some flexibility.

License

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

Share

About the Author

Albin Abel
Software Developer
India India
I am developer in .Net and GIS. albin_gis@yahoo.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 29 Apr 2011
Article Copyright 2011 by Albin Abel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid