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 (visible=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?
A:
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.
Matthew Jones
2010-04-13 20:08:35
Please note that this is C# syntax. You will need to change this to fit VB syntax. Also note that, according to the link, this will only work if the asp:Image is not inside a ContentPlaceHolder. If it is inside one, you must find the ContentPlaceHolder first then use FindControl on that to find the asp:Image.
Matthew Jones
2010-04-13 20:10:55
-1: IMO - letting the page know the "name" or the hierarchy of the control as expressed in the Master page is not good programming practice. Any change to the control name and/or its location will impact all the code. So I don't like the MSDN sample too :)
Sunny
2010-06-13 04:25:23
A:
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
Sunny
2010-06-13 04:18:07