Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Method in Master Page

I have a public method in my ASP.NET Master Page. Is it possible to call this from a content page, and if so what are the steps/syntax?

like image 914
Adrian S Avatar asked Jun 13 '11 15:06

Adrian S


People also ask

What is the difference between master page and nested master page?

The main difference between the nested master page and a content page bound to the same top-level master page is that the nested master page can include ContentPlaceHolder controls. The nested master page's ContentPlaceHolder controls define the regions where the content pages can customize the markup.

What does a master page contain?

What is a Master Page? A Master Page is a nonprinting page that you can use as the template for the rest of the pages in your document. Master pages can contain text and graphic elements that will appear on all pages of a publication (i.e. headers, footers, page numbers, etc.)

What is master page explain with example?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary . aspx pages.


4 Answers

From within the Page you can cast the Master page to a specific type (the type of your own Master that exposes the desired functionality), using as to side step any exceptions on type mismatches:

var master = Master as MyMasterPage;
if (master != null)
{
    master.Method();
}

In the above code, if Master is not of type MyMasterPage then master will be null and no method call will be attempted; otherwise it will be called as expected.

like image 77
Grant Thomas Avatar answered Oct 02 '22 17:10

Grant Thomas


Use the MasterType directive like e.g.:

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

Then you can use the method like this:

Master.Method();
like image 45
Uwe Keim Avatar answered Oct 02 '22 17:10

Uwe Keim


You can simply do like...

MasterPageClassName MasterPage = (MasterPageClassName)Page.Master;
MasterPage.MasterMethod();

Check for Details ACCESS A METHOD IN A MASTER PAGE WITH CODE-BEHIND

like image 38
Muhammad Akhtar Avatar answered Oct 02 '22 16:10

Muhammad Akhtar


MyMasterPageType master = (MyMasterPageType)this.Master;
master.MasterPageMethod();
like image 33
George Duckett Avatar answered Oct 02 '22 17:10

George Duckett