简体   繁体   中英

access function in aspx page to master page

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

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?

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");
        }

        ....
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM