Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET HyperLink - Setting Alt tag of the Image

Tags:

asp.net

How can I set the alt tag of an image that has been set using HyperLink.ImageUrl? I read an article that states you should be able to do HyperLink.Attributes["text"] = "My Alt Text" but that doesn't seem to work.

I want to try and avoid creating a separate image control and adding it to that hyperlink just to set an Alt tag.

Thanks.

like image 684
webnoob Avatar asked Oct 28 '25 15:10

webnoob


2 Answers

asp:Hyperlink already has a "Text" property. Just set it and that will serve as the alt property of the image if you have the ImageUrl set.

ETA: I haved edited my answer based on Andrew MacNeill's suggestion below to show some example code.

Example:

hyperLink.Text = "My Alt Text"; 
hyperLink.NaviateURL = "www.myurl.com";
hyperLink.ImageURL = "myimage.jpg";

Renders HTML as:

<a href="www.myurl.com">
    <img src="myimage.jpg" title="My Alt text" alt="My Alt Text">
    </img>
</a>
like image 68
Wayland Young Avatar answered Oct 31 '25 13:10

Wayland Young


it could work with the Attributes as you have mentioned but you should set such attribute in the PreRender event of the page or of the HyperLink, if you set it before the PreRender you will most likely lose it.

like image 25
Davide Piras Avatar answered Oct 31 '25 11:10

Davide Piras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!