0

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

ksg
  • 3,927
  • 7
  • 51
  • 97
  • 1
    is is child form i.e using master page ?? – Pranay Rana May 21 '12 at 10:22
  • [See here](http://stackoverflow.com/a/7738865/263681): the question differs slightly but the solution remains the same. Or [here](http://stackoverflow.com/q/6332889/263681) is a more exact question, [same kind of answer](http://stackoverflow.com/a/6332933/263681). – Grant Thomas May 21 '12 at 10:23
  • possible duplicate of [Get Textbox Value from Masterpage using c#](http://stackoverflow.com/questions/7738693/get-textbox-value-from-masterpage-using-c-sharp) or [Call Method in Master Page](http://stackoverflow.com/q/6332889/263681). – Grant Thomas May 21 '12 at 10:23
  • A further note is that none of the current answers to this question are 'safe' - both answers linked above are. – Grant Thomas May 21 '12 at 10:27
  • See my answer below. I converted this to C#. You should be able to use it as a guide. Good luck! Happy coding! – Emmie Lewis-Briggman May 21 '12 at 10:33
  • Yeah,answers are related to 'calling a masterpage function which is the child of above masterpage'.I want to call a masterpage function from a which is not the child of the masterpage – ksg May 21 '12 at 11:40
  • @ksg: I've updated [my answer there](http://stackoverflow.com/a/10683524/284240) to take this more into account. I short: it's not possible since the master will not be initiliazed when it is not used. – Tim Schmelter May 21 '12 at 11:55
  • @Mr.Disappointment: Define "safe", imho it's safer to raise an error immediately as soon as i'm explicitely trying to call a method that is not accessible. Apart from that, OP actually tries to access a master that is **not** the master of the current page. – Tim Schmelter May 21 '12 at 12:17
  • @TimSchmelter 'safe' as in exceptions being raised, which can easily be avoided. As for how to handle errors, that's a design decision. And the error isn't on calling an undefined method, but casting from a `Master` to a custom master page derivative, when the `Master` isn't of that type. – Grant Thomas May 21 '12 at 12:18
  • @Mr.Disappointment: But wouldn’t it be the _safest_ approach simply not to call that method? – Tim Schmelter May 21 '12 at 12:21
  • @TimSchmelter By that logic the _safest_ approach would be not to program anything. If the method is needed, then it should be called; if it can't be accessed (because the type is wrong or whatever) then failing gracefully is possible. There might be reasons for letting it just explode. I'm just saying. – Grant Thomas May 21 '12 at 12:21
  • @Mr.Disappointment: You haven't understood the point. You would prefer a method that ostensively gets called(as intended) but actually is not executed at all? I would prefer to be alerted if i (accidentially) call an inaccessible method, so that i can remove that redundant call or use another approach. Anyway, it might be a matter of taste. – Tim Schmelter May 21 '12 at 12:28
  • @TimSchmelter No, the point is why would you call an inaccessible method? Indeed, how could you? From where we are, you wouldn't get that far is the Master is not of the same type as which you want to cast it. The bottom line is that this can easily go _BANG!_, when it need not: `((SiteMaster)this.Page.Master).DataBindDropDowns();` If it were strongly-typed using the markup directive then this is avoided, it can also be avoided when using `as`. Both can be handled gracefully, the other could use a `try`/`catch`, or could just break (a possible desire, granted). – Grant Thomas May 21 '12 at 12:30
  • Of course other options can still fail, but it either fails faster or there is a deeper problem. – Grant Thomas May 21 '12 at 12:52

7 Answers7

2

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.

  • As all answers here(mine too), it's not what OP is asking. He wants to call a function in a master that is **not** the master of the current page. – Tim Schmelter May 21 '12 at 11:58
1

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:

  • The master's method is static, what is impossible in this use case since you want to bind controls on the master
  • You have a reference to a page which master is of that type, but again impossible since the current 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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

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
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • Your null reference check doesn't make this 'safe', an exception will still occur if the master is not of type `MyMasterPage` - which is a frequent possibility. – Grant Thomas May 21 '12 at 10:29
0

You need to refer to MasterPage property, cast to your master page type and invoke your method.

((MyMasterPage)this.Master).MyBindingFunction();
Samich
  • 29,157
  • 6
  • 68
  • 77
0

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; 
        }
    }
JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

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()
2GDev
  • 2,478
  • 1
  • 20
  • 32
0

For better design use EventAggregator pattern. Create your custom event and handle it in Master Page.

PaulSh
  • 24
  • 2