Changing elements in master page from content page in vb.net
i have a page called a1.aspx, with the Masterpagefile = a1_master.master. Now the master page has its own divs and images for design purposes. I want a way where when i load a1.aspx, certain chosen 's and images should be hidden (visi开发者_如何学Goble=false). how can i do this? how can i change the visibility of a div or an image in the master page from the content page?
Declare an Interface for your master page like:
interface IMasterPageControls{
Image MyImage { get; }
}
& implement it on your master page:
public class MasterPage : IMasterPageControls{
public Image MyImage {
get {
// whatever it takes to get the correct object...
return (Image)this.Page
.FindControl("nameOfContainer")
.FindControl("nameOfImage");
}
}
}
& then in your page you could do:
Image img = (this.Master as IMasterPageControls).MyImage;
this will give you the handle of the image & remove some of the problems which @Matthew mentions...
HTH
Probably you want to use FindControl and Master, like so:
Image myImage = (Image)Master.FindControl("nameOfImage");
myImage.Visible = false;
Check this MSDN page for more information and samples.
Create a property for the master page
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; }
}
}
In your content page:
(this.Master as YourMasterPage).HideImage = true;
精彩评论