Reputation: 1070
I declared an picture in my ASP.NET and set it to hidden by default with style="visbility:hidden;"
. Is there anyway to access this image from the C# and change its visibility? Here is the img line from the ASP.NET:
<img src="canoe.png" alt="Boat Trailer" height="350px" width="600px" id="canoe" style="float:right; margin-right: 100px; visibility:hidden;" />
Upvotes: 2
Views: 9863
Reputation: 12561
Option 1
You can change it on the server side by adding an ID attribute and runat='server'
.
Option 2
You can use an ASP.NET Image control and apply your changes there.
<asp:Image id="myimg" runat='server'.../>
HOWEVER, if you go with this option, you should still use CSS/Javascript to show/hide your image since setting Visible='false'
on a server side control will prevent the HTML from rendering altogether which I doubt is the output your are expecting.
If you are performing this logic on the server side, why do you need to render the image as hidden? If you are showing the image based on user input then you should do it on client side Javascript.
Upvotes: 0
Reputation: 3959
You'll need to make this into a server tag first by adding runat='server'
and an id. Then you can change its properties before the page loads like so:
<IMG ID>.Style["visibility"] = "visible";
replacing with the ID of your element.
Note about other answers: C# Visibility property will not change the CSS visibility but will actually remove or add the element, if you simply change that while the element is still set to visibility: hidden
then it will still not be visible
Discalimer, this is an untested solution, you may have to check the syntax on the use of the Style property
Upvotes: 0
Reputation: 29
If it is just server side to control the image visibility, just use,
<asp:Image ID="Image1" runat="server" Visible="false" />
Upvotes: 0
Reputation: 238
Use ASP:Image instead.
<ASP:Image id="myImage" Visibile="False" ImgUrl="link" runat="server">
Then you can access it in the backend with:
myImage.Visible = true;
Upvotes: 2