开发者

ASP.NET HyperLink - Setting Alt tag of the Image

开发者 https://www.devze.com 2023-02-10 14:09 出处:网络
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[\"tex开发者_开发技巧t\"] = \"My Alt Text\"

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["tex开发者_开发技巧t"] = "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.


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>


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.


From codebehind, you can use something like this:

HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
HyperLink1.ImageUrl = "Images\\Success.png";
HyperLink1.ToolTip = "Completed";

The ToolTip property will map to the alternate text for the image.

0

精彩评论

暂无评论...
验证码 换一张
取 消