A page could have an attribute MasterType
to make Page.Master
to be strong-typed:
<%@ MasterType VirtualPath="~/Site.master" %>
If all my pages inherits specific class inherited System.Web.UI.Page
itself, can I someway get access to this.Master
property in it so I could call master page's methods?
To set the master page programmatically, then, we can either create an event handler for the PreInit event or override the OnPreInit method.
To reference a control on the master pageUse the FindControl method, using the value returned by the Master property as the naming container. The following code example shows how to use the FindControl method to get a reference to two controls on the master page, a TextBox control and a Label control.
For this go to the Solution Explorer and "Add new item" then select "webform" and give a name to the web page. In this case I have named it "myPage. aspx". Check the checkbox "select master page" and click the "Add" button.
In your base class that all pages inherit from, just override the Master
property, something like this:
public new SiteMaster Master
{
get { return base.Master as SiteMaster ; }
}
or abatishchev's own variant:
public new ISiteMaster Master
{
get { return base.Master as ISiteMaster; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With