I've a Master Page which contains a DropDownList
. I've a function for binding the list in the master and it works fine.
My problem is: How will I call that Master Page function from a form, which is not the child of the above master page
I've a Master Page which contains a DropDownList
. I've a function for binding the list in the master and it works fine.
My problem is: How will I call that Master Page function from a form, which is not the child of the above master page
See article here.
Here is something nice that comes from the new compilation model in ASP.NET 2.0. Let’s say you add a custom property to a master page code-behind file like so:
partial class otcMaster : System.Web.UI.MasterPage
{
public string FooterText {
get { return Footer.Text; }
set { Footer.Text = value; }
}
}
You can get to the master page for a web form using the inherited Master property, which returns a MasterPage reference. To get to a property defined in otcMasterPage though, you might think you need to use a cast.
((otcMaster)Master).FooterText == "foo"
Casting to a derived type is just a part of life when using frameworks and statically typed languages, but there is a better way. Use the @ MasterType directive in the ASPX.
<%@ MasterType VirtualPath="~/otc.master" %>
Now when ASP.NET codegens the page, it puts the following inside a partial class definition. Notice the Shadows keyword (that would be the new keyword in semicolon land [yeah, I’m experimenting with alternative languages]).
public new otc Master {
get { return (otcMaster)base.Master; }
}
The result is a strongly typed Master Page. We don’t need a cast, we can go right to the Master.FooterText property. Another way to do this is to specify a TypeName in the @MasterType directive.
Provide a public method in your MasterPage
, then you need to cast the ContentPage's Master
property to the appropriate type:
public void DataBindDropDowns()
{
// ...
}
Then you can call it from your ContentPages in the following way(assuming your masterpage's type is called SiteMaster
:
((SiteMaster)this.Page.Master).DataBindDropDowns();
Edit:
...which is not the child of the above master page
I assume that means it's no a ContentPage
of that Master, am i right?
Then it's not possible to get a reference to the master except when:
HTTP-Handler
is another page which does not use this master Note that the master page actually is a child of a ContentPage
and will be merged with it. It's not possible to get a reference to an object that does not exist!
From MSDN:
Note that the master page becomes a part of the content page. In effect, the master page acts in much the same way a user control acts — as a child of the content page and as a container within that page.
Put this in your page code (where MyMasterPage is your master page object):
MyMasterPage masterPage = (MyMasterPage) this.Master;
masterPage.MyBindDropDownListFunction(); // Replace with your public function name
You need to refer to MasterPage property, cast to your master page type and invoke your method.
((MyMasterPage)this.Master).MyBindingFunction();
If you're making it very frequently, you can create a BasePage
derived from System.Web.UI.Page
, and use it as the bage page for your forms.
There you can add a property of the type of your master page, that will give you acces to all public members of the Master Page.
If you master page class is Site1
, you could do something like this in your BasePage
.
public class BasePage : System.Web.UI.Page
{
protected Site1 Site1Master
{
get { return Master as Site1; }
}
}
Then in the pages where you need to acces the methods of the master page replace:
public partial class DefaultPage : System.Web.UI.Page
with
public partial class DefaultPage : BasePage
Then you'll have the property Site1Master available in the pages, and you can use any of its public members like this:
Site1Master.MyBindingFunction(...);
You can also add any other desired functionality in your BasePage.
NOTE: If you want to make sure that the property isn't null in the pages, you can add a check to see if the page has the Site1
master, like this:
protected Site1 Site1Master
{
get
{
if (!(Master is Site1))
throw new Exception("This page doesn's have Site1 as master page");
return Master as Site1;
}
}
For accessing the members of a Master page there's a Master property exposed on Page Content. First you've to specify the @ MasterType directive :
<%@ Page masterPageFile="~/MasterPage.master"%>
<%@ MasterType virtualPath="~/MasterPage.master"%>
Then in the Master Page create a Public function and in your content Page you simply call
Master.MethodNameInMaster()
For better design use EventAggregator pattern. Create your custom event and handle it in Master Page.