简体   繁体   中英

Access MasterPage public property in Design

I am setting a PATH for images in my web.config file. On each page, I am setting a public property like:

  public string ImgPath { get { return Config.CDNImages; } } 

Then in the design page I use

<img src='<%#ImgPath%>/products/<%#Eval("prodID")%>.jpg'/></a>

It works, but now I want to add this public property on the MasterPage so I can access ImgPath on every page.

I cant seem to get the <%#ImgPath%> to work when the property in on the MasterPage. I have tried <%#Master.Page.ImgPath%> .. and many other variation, but I cant seem to get it to work.

How do I get the public property within a MasterPage on a design page using in-line request/binding?

I don't think this will work:

<%#Master.Page.ImgPath%>
 or
<%#Page.Master.ImgPath%>

Because Page.Master returns a base class, not the specific class of your master page, which means the ImgPath property won't be available without casting the Page.Master object. That would get very tedious to do every time.

Instead of adding the property to your master page, have you considered doing something like this?

public abstract class YourPage : Page
{
    public string ImgPath
    {
        get { return Config.CDNImages; }
    }
} 

Now change your pages so they inherit from YourPage instead of Page , and you should have access to your method on every page.

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