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?
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 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.)
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.
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.
Use the MasterType
directive like e.g.:
<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>
Then you can use the method like this:
Master.Method();
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
MyMasterPageType master = (MyMasterPageType)this.Master;
master.MasterPageMethod();
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