MasterType directive in Content Page

In this blog post, An example will be provided to demonstrate the benefit of the MasterType directive.

Suppose we have a page associated with a master page. In the master page, we have a Label control (Label1) which we need to access its text property and change it from the content page. We create a public property in the master page called LabelText 

    public string LabelText
    {
        get
        {
            return Label1.Text;
        }
        set
        {
            Label1.Text = value;
        }
    }

To gain access to the master page file in the content page, Add the below directive to the content page

<%@ MasterType VirtualPath="~/MasterPage.master"%>

When ASP.NET realizes the existance of this directive, it generates a strongly typed property "Master". this property will give you access to the MasterPage file. So directly in the content page code behind you would use the below instead of trying to use the FindControl method.

Master.LabelText = "Testing the MasterType directive effect";

Hope this helps 

 

Comments

No Comments

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.