应用CSS类到图像中的asp:超链接? [英] Apply CSS Class to Image in asp:Hyperlink?

查看:109
本文介绍了应用CSS类到图像中的asp:超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP:超链接来呈现动态基于URL参数链接的图像。我需要能够以一个CSS类添加到处理img,并不能弄清楚如何做到这一点。

我知道我可以添加的CssClass =嗒嗒到ASP:超链接,但在呈现的HTML中,只有一个接收到的CSS类。像这样的:

 < A HREF =资产/图像/ blah.jpg级=嗒嗒ID =ctl00_LeftContent_alternateImage4>
< IMG风格=边界宽度:0像素; SRC =资产/图像/ blahThumbnail.jpg/>
&所述; / A>

我发现<一个href=\"http://stackoverflow.com/questions/92427/apply-inline-css-to-an-asp-net-server-control\">another 这个问题让我内联样式添加到控件,但我想一个类添加到IMG的ASP:超链接生成

是否有可能做一些类似这样的回答了一句:

  myControl.Attributes.Add(风格,颜色:红色);

像,也许?

  myControl.img.Attributes.Add(类,嗒嗒);


解决方案

它看起来像你使用超链接的ImageUrl属性。我会建议建立明确的内部图像控制:

 &LT; ASP:超链接=服务器的CssClass =linkclassNavigateUrl =htt​​p://example.com&GT;
   &LT; ASP:图像=服务器的CssClass =imgClass的ImageUrl =yourimage.jpg/&GT;
&LT; / ASP:超链接&GT;

I'm using asp:Hyperlink to render linked images dynamically based on parameters in the URL. I need to be able to add a CSS class to the rendered img, and can't figure out how to do that.

I know I can add "CssClass="blah"" to the asp:Hyperlink, but in the rendered HTML, only the a receives the css class. Like this:

<a href="assets/images/blah.jpg" class="blah" id="ctl00_LeftContent_alternateImage4">
<img style="border-width: 0px;" src="assets/images/blahThumbnail.jpg"/>
</a>

I've found another question that allows me to add inline style to a control, but I want to add a class to the img that asp:Hyperlink generates.

Is it possible to do something similar to this answer:

myControl.Attributes.Add("style", "color:red");

Like, maybe?:

myControl.img.Attributes.Add("class", "blah");

解决方案

It looks like you're using the ImageUrl property of HyperLink. I would recommend creating the inner image control explicitly:

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

这篇关于应用CSS类到图像中的asp:超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆