Saturday, March 31, 2012

Setting ImageUrl for Image on Master Page

Hi,

I have a master page which includes a header, a navigation system and a ContentPlacHolder.

In the header is an <asp:Image> which I have exposed via a property, and I need to (optionally) set it's ImageUrl per each content page that uses the master.

So far, just to learn the concept, I've successfully set the ImageUrl via code in a few content pages:

this.Master.MasterImage.ImageUrl ="images/image01.jpg"

Eventually, I'll have many pages, so I would like to automate this process. Maybe look up the image in a database or something ...

Is there a way/place (maybe some global event or something?) to place some code (I can make the code) that runs every time each content page is loaded? Also, can I specify that itonly happens if the content page has a specific master page?

I'm just trying to make it so that others wont have to add the above line of code to each new content page that is made in the future. If others are adding content pages, they don't need to access the code-behind. All they will need to do is define the url in a database or something.

I'm using the ASP 2 beta, so maybe it has something new?

Thanks!

Hello.
well, i was tempted to suggest the creation of a custom master page that would add that line on the load event of the page but if you try that you'll have a dependency problem.
here's my suggestion:
create a new aspx file which doesn't have any controls on it. comething like this:

<%@.PageLanguage="C#" %>
<%@.ReferenceControl="~/MasterPage.master" %>
<scriptrunat="server">
protectedoverridevoid OnLoad(EventArgs args )
{
if (this.Master !=null )
{
if (this.Masteris ASP.MasterPage_master )
{
//your code here
}
}
}
</script>

Then you could build all your pages by inherting from that 1st aspx page. something like this:

<%@.ReferencePage="~/Default3.aspx" %>
<%@.PageLanguage="C#"Inherits="ASP.Default3_aspx"MasterPageFile="~/MasterPage.master"Debug="true" %>
<asp:Contentrunat="server"ContentPlaceHolderID="ContentPlaceHolder1"ID="p">
safsdf
</asp:Content>

This is the best I can think of. You still have to add the @.Reference tag to all your pages but you won't have to write any code for setting the master property.

0 comments:

Post a Comment