iJK
iJK

Reputation: 4755

Adding a class attribute on the Image tag generated by the hyperlink control

I have a hyper link control and I set the NavigateURL and the ImageURL property at runtime. I also need to set the class of the image tag that it generates but I cannot figure out how I can do that. The solution mentioned here

Apply CSS Class to Image in asp:Hyperlink?

does not work because the image url is hard coded.

any ideas?

Upvotes: 0

Views: 1583

Answers (2)

OyvindK
OyvindK

Reputation: 110

I'm not sure if there is a way to set the CssClass directly like this, but a workaround is to dynamically create an Image, and add it to the HyperLink's control collection, like so:

Image _img = new Image();
_img. ImageUrl = "image.jpg";
_img.CssClass = "myClass";
HyperLink1.Controls.Add(_img);

Upvotes: 0

brendan
brendan

Reputation: 29976

You should still be able to use that solution and just dynamically assign the image:

<asp:HyperLink runat="server" CssClass="linkclass" NavigateUrl="http://example.com">
   <asp:Image runat="server" Id="ImageLink" CssClass="imgClass" ImageUrl="paceholder.jpg" />
</asp:HyperLink>

Then in code behind you can easily set:

ImageLink.ImageUrl = "MyDynamicImage.jpg";

Upvotes: 3

Related Questions