我有一个名为a1.aspx的页面,其中Masterpagefile = a1_master.master。现在,母版页有自己的div和图像用于设计目的。我想要一种方式,当我加载a1.aspx时,某些选择的和图像应该被隐藏(可见=假)。我怎样才能做到这一点? 如何从内容页面更改母版页中div或图像的可见性?
答案 0 :(得分:1)
为您的母版页声明一个接口,如:
interface IMasterPageControls{
Image MyImage { get; }
}
&安培;在您的母版页上实现它:
public class MasterPage : IMasterPageControls{
public Image MyImage {
get {
// whatever it takes to get the correct object...
return (Image)this.Page
.FindControl("nameOfContainer")
.FindControl("nameOfImage");
}
}
}
&安培;然后在你的页面中你可以做到:
Image img = (this.Master as IMasterPageControls).MyImage;
这将为您提供图像和图像的处理。删除@Matthew提到的一些问题......
HTH
答案 1 :(得分:0)
您可能希望使用FindControl和Master,如下所示:
Image myImage = (Image)Master.FindControl("nameOfImage");
myImage.Visible = false;
检查this MSDN page以获取更多信息和样本。
答案 2 :(得分:0)
为母版页创建属性
public partial class YourMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{ }
public bool HideImage
{
get { return imgYourImage.Visible; }
set { imgYourImage.Visible = value; }
}
}
在您的内容页面中:
(this.Master as YourMasterPage).HideImage = true;