Refreshing the master page image from the content page

Have made several attempts to update the image in the master page div when navigating between content pages. I first tried to create a master page property that I could update using the Page Load page by declaring "MasterType VirtualPath =", but since the wizard has already loaded by this time, it won't work. It worked when I set ImageUrl on the homepage page load event (if! Page.IsPostBack then set the url attribute of the image), so I know it might work, but I need the image to change for every content page I visit ...

Then I tried using the main menu buttons to set the ImageUrl before loading the content page, but that also had no effect. I saw a thread suggesting to use UpdatePanel to store the image, so I can try the following. What's the best way to do this ???

I wouldn't be surprised if it was better to have the image in the content div instead and update it from the master. Any suggestions or links would be most welcome. I can post the code if anyone wants to take a look. thank.

+3


source to share


1 answer


I don't know why it was difficult for you. There are many ways to do this, but I'll only show you one. I just tested this and it worked. How?

On the main page, define your image and add runat="server"

<img src="put your default image.jpg" runat="server" id="changingImage" />

      

On your content pages, do this



protected void Page_Load(object sender, EventArgs e)
{
    HtmlImage img = Master.FindControl("changingImage") as HtmlImage;

    img.Src = "~/images/imageForContentPage1.jpg"; //replace this image based on your criteria

}

      

Possible exception is Null Reference

if the name of the image control specified in .FindControl

cannot be found. Make sure it is exactly what you named it on the homepage. And to prevent Null Reference Exception, wrap your check around

if(img != null)
{
    img.Src = "~/images/imageForContentPage1.jpg";
}

      

+3


source







All Articles