Access master page control


Here is how to access a master page control from a content page:

1) In the code behind for the master page, declare a public property for each control you want to expose. 

For example, let’s say we have the following table cell in the master page:

<td runat="server" id="tdTableCell" visible="false">  cell contents  </td>

And let’s say would like to make the table cell visible / invisible from a content page.  Let’s go ahead and declare a public property for the table cell control in the code behind in the master page.  Make sure to match the id of the control in the get /set.

public HtmlTableCell MyTableCell
{
    get
    {
        return tdTableCell;
    }
    set
    {
        tdTableCell= value;
    }
}

2) Now that we have our control exposed in the master page, we need to add the following line to the front end of the content page.  Just make the virtual path the same value as your MasterPageFile in your page directive:

<%@ MasterType  virtualPath="~/MyVirtualPath.Master"%>

3) Now you will have access to the master page control in the code behind of the content page.  For example:

private void Page_Load(object sender, System.EventArgs e)
{
   if (!IsPostBack)
    {
        this.Master.MyTableCell.Visible = true;

No comments:

Post a Comment