Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set MasterType programmatically?

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?

like image 507
abatishchev Avatar asked Feb 12 '10 01:02

abatishchev


People also ask

Which events can be used to programmatically set the master page file for an asp net page?

To set the master page programmatically, then, we can either create an event handler for the PreInit event or override the OnPreInit method.

How to reference master page in ASP net?

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.

How to access master page function in content page ASP net?

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.


1 Answers

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; }
}
like image 55
Nick Craver Avatar answered Oct 04 '22 03:10

Nick Craver