我有一个网格视图,它列出了来自 SQL 的信息行。如果后期列设置为 1,它应该以红色显示该行的文本。这可行,但是超链接文本不会以红色显示。所以我正在尝试a)更改超链接元素的前景色或b)将类应用于元素。当我尝试检索超链接时,它没有得到它。当我检索标签时,它似乎工作正常。
ASP
<asp:TemplateField HeaderText="Project">
<ItemTemplate>
<a id="hlProject" href="VpnDetails.aspx?Project=<%# Eval("id") %>"><%# Eval("project") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Update">
<ItemTemplate>
<asp:Label ID="lblLastUpdate" runat="server" Text='<%#Eval("diff") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
C#
protected void gvLastIp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Adds the tooltip to the last update label
Label lblLastUpdate = e.Row.FindControl("lblLastUpdate") as Label;
DateTime activeSince = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "begindate"));
DateTime lastupdate = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "lastupdate"));
lblLastUpdate.ToolTip = "Active Since " + activeSince.ToString("MMMM d yyyy HH:mm") + " - Last Update " + lastupdate.ToString("MMMM d yyyy HH:mm");
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "late")) == "1")
{//if the row is late it should be red
e.Row.Font.Bold = true;
e.Row.ForeColor = System.Drawing.Color.Red;
HyperLink hlProject = new HyperLink();
try
{
hlProject = (HyperLink)e.Row.FindControl("hlProject");
hlProject.Attributes.Add("class", "late");
hlProject.ForeColor = System.Drawing.Color.Red;
}
catch (Exception e1)
{
lblError.Text = e1.Message;
}
}
}
所以我需要知道为什么它适用于标签而不适用于超链接。我还需要超链接的解决方案。