如何在GridView单元格中设置链接颜色

时间:2015-12-22 15:03:03

标签: css asp.net vb.net gridview

ASP.net:

<asp:HyperLinkField DataNavigateUrlFields="carrier_id,carrier_plan_id,card_id" HeaderText="Health Plan" 
         HeaderStyle-CssClass="subNav" HeaderStyle-HorizontalAlign="Center" SortExpression="health_plan" HeaderStyle-BackColor="#CCCCCC"
         DataNavigateUrlFormatString="somewhereA.aspx?cid={0}&cpid={1}&ccid={2}" DataTextField="health_plan_title"
         NavigateUrl="somewhereA.aspx" ItemStyle-Font-Bold="True">
            <HeaderStyle CssClass="infoBoldBlueSmall"></HeaderStyle>
     </asp:HyperLinkField>

默认情况下,链接颜色是由浏览器标准驱动的,但我有条件,如果遇到,我希望链接颜色改变,这是在代码隐藏中完成的:

If IsDBNull(e.Row.DataItem("isRed")) = False Then
    If (e.Row.DataItem("isRed") = "N") Then 'AND (lnk.NavigateUrl Is Nothing) Then
        e.Row.Cells(1).ForeColor = Drawing.Color.Red //hyperlink (which doesn't work because it is only setting the cell color)
        e.Row.Cells(1).CssClass = "redColor" //hyperlink (which also doesn't work because it is being overridden by browser)
    End If
End If

CSS:

.redColor
{
    color: #FF0000 !important;
}

浏览器输出:

<td class="redColor" style="color: red; font-weight: bold;"><a href="somewhereA.aspx?cid=90000&amp;cpid=12&amp;ccid=56">This should be red</a></td>

将TD设置为红色,而不是链接本身。

如果符合条件,如何修改我的VB.net代码,将链接类设置为redColor

1 个答案:

答案 0 :(得分:1)

调整CSS将解决它。你的VB代码可以保持不变。

.redColor, .redColor a
{
    color: #FF0000 !important;
}