tags:

views:

104

answers:

4

hello folks.,

I have a.master and b.aspx .

i have some functions in my aspx page.

how to access that functions in a.master page.

thank you

+1  A: 

Of course you can make the method in b.aspx be public to call it in a.master. However I suggest you consider your design carefully. Because it's really weird just like that you call a method of a child class from its parent class (even though it's theoretically possible). Before your modification, ask yourself:

Is it necessary to call this method in the master page? If yes, do I have a better place to put the method?

Danny Chen
+1  A: 

Let's say, you want to call Foo from b.aspx from a.master. So first thing is that you have make the method internal (or public) and then you can use code such as below in master page is call that method.

var page = (b)this.Page;
page.Foo();

Note that b will be the code behind class name in b.aspx. Note that above code will fail if you use another page c.aspx and use the same master a with it. Generally, I will say that invoking page specific functions from master does not make sense unless functions are present in some base page class and in such case you should be casting to that base page class.

Edit: More elaborate example as requested by Asif:

Consider your content page b.aspx such as

<%@ Page Language="C#" MasterPageFile="a.Master" Title="Page B" AutoEventWireup="true"
    CodeBehind="b.aspx.cs" Inherits="YourProject.b" %>

And in code behind file (b.aspx.cs), you have a method Foo such as

namespace YourProject
{
    public partial class b : System.Web.UI.Page
    {
            void Foo(string someParameter)
            {
                Label1.Text = someParameter
            }
        ...
    }
}

Now in code behind (a.master.cs) of a.master page

namespace YourProject
{
    public partial class a : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           b contentPage = (b)this.Page;
           contentPage.Foo("Hello");
        }

        ....
    }
}
VinayC
This is the general idea, but the (b) can not find from master page... did you make a test to see ?
Aristos
If b.aspx uses the master page then Page property of Master will give the reference of the b page object. This is simply because master page is nothing but a control on the actual page.
VinayC
This is not working, you talk in theory, but you did not try it.
Aristos
@Aristos, not sure what you make think so! I have a couple of web applications that does exactly what I have illustrated except they refer to a common page base class rather than a page specific class (because that would force the master page to work with that specific page only). Perhaps, I should ask you did you try it and if yes, what is the error that you get?
VinayC
@Aristos, this definately works as advertised. Maybe you should explain the problem you are having, it might be that you have misunderstood what was said.
Chris Taylor
if you can please tell me with an example as how to pass parameter value to content page method from master page in vs2005?
Asif khan
@Chris, @VinayC Maybe I misunderstander I will read it and check it again.
Aristos
+1  A: 

As others have said, it's possible to do this. However, it's an odd way of doing things. You are probably going to be better off doing whatever you want to do in a different way. The whole idea of a master page is that it "wraps" many kinds of content pages. What if you content page doesn't have the function you want to call?

You could make sure all your content pages have the function, but then why not just put it in the master page?

Perhaps if you descired what you wanted to do a little better, we could advies you on a better way to handle things.

Mystere Man
A: 

To access, either:

  1. Make that method a static method.
  2. Move your code in App_Code folder.
  3. Move your code out of your web project, into some generic assembly and use that as a reference.
KMan