Have made several attempts to update an image in a master page div when switching between content pages. I first tried creating a master page property that I could update from the content page's Page Load event by declaring "MasterType VirtualPath=", but since the master is already loaded by this time it wasn't going to work. It did work when I set the ImageUrl in the master page's page load event (if !Page.IsPostBack then set the image's url attrib), so I know it can work, but I need the image to change for every content page I visit.
在内容页之间切换时,多次尝试更新主页div中的图像。我首先尝试创建一个master page属性,可以通过声明“MasterType VirtualPath=”从content page的页面加载事件中更新这个属性,但是由于这个master现在已经被加载了,所以它不会工作。当我在主页的页面加载事件(if ! page)中设置ImageUrl时,它确实有效。IsPostBack然后设置图像的url attrib),所以我知道它可以工作,但是我需要在每次访问内容页面时修改图像。
Then I tried using the master's menu button click events to set the ImageUrl before loading the content page but this also had no effect. I saw a thread suggesting the use of an UpdatePanel to hold the image, so I may try that next. What's the best way to do this..??
然后在加载内容页面之前,我尝试使用master的菜单按钮click events设置ImageUrl,但这也没有效果。我看到一个提示使用UpdatePanel来保存图像的线程,因此接下来我可以尝试一下。做这件事最好的办法是什么?
I wouldn't be surprised if a better way is to have the image in a content div instead, and updating that from the master. Any suggestions or links would be most welcome. I can post code if anyone would like to have a look. thanks.
如果一个更好的方法是在内容div中使用图像,并从主服务器更新它,我不会感到惊讶。任何建议或链接将是最受欢迎的。如果有人想看的话,我可以贴代码。谢谢。
1 个解决方案
#1
3
I don't know why you found it difficult. There are many ways to do this but I'll only show one. I just tested this and it worked. How?
我不知道你为什么觉得这很难。有很多方法可以做到这一点,但我只展示一个。我刚刚测试过这个,它成功了。如何?
In your master page, define your image and add runat="server"
在主页面中,定义映像并添加runat="server"
<img src="put your default image.jpg" runat="server" id="changingImage" />
In 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
when the name of the image control specified in .FindControl
could not be found. Make sure it's exactly as you named it in the Master Page. And to prevent a Null Reference Exception, wrap a check around
当无法找到. findcontrol中指定的图像控件的名称时,可能的异常是空引用。请确保它与您在主页中指定的名称完全一致。为了防止空引用异常,请将检查包括起来。
if(img != null)
{
img.Src = "~/images/imageForContentPage1.jpg";
}
#1
3
I don't know why you found it difficult. There are many ways to do this but I'll only show one. I just tested this and it worked. How?
我不知道你为什么觉得这很难。有很多方法可以做到这一点,但我只展示一个。我刚刚测试过这个,它成功了。如何?
In your master page, define your image and add runat="server"
在主页面中,定义映像并添加runat="server"
<img src="put your default image.jpg" runat="server" id="changingImage" />
In 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
when the name of the image control specified in .FindControl
could not be found. Make sure it's exactly as you named it in the Master Page. And to prevent a Null Reference Exception, wrap a check around
当无法找到. findcontrol中指定的图像控件的名称时,可能的异常是空引用。请确保它与您在主页中指定的名称完全一致。为了防止空引用异常,请将检查包括起来。
if(img != null)
{
img.Src = "~/images/imageForContentPage1.jpg";
}