简体   繁体   English

在Gridview RowDatabound期间找不到单元格的超链接

[英]Can't Find Hyperlink of Cell during Gridview RowDatabound

I have a gridview that lists rows of information from SQL. 我有一个gridview列出了来自SQL的信息行。 If the late column is set to 1 it should display the text of the row in red. 如果后期列设置为1,则应以红色显示该行的文本。 This works, however the hyperlink text will not be displayed in red. 此方法有效,但是超链接文本不会以红色显示。 So I'm trying to a) change the forecolor of the hyperlink element or b) apply a class to the element. 因此,我正在尝试a)更改超链接元素的前景色或b)将一个类应用于该元素。 When I try to retrieve the hyperlink it does not get it. 当我尝试检索超链接时,它没有得到它。 When I retrieve a label it seems to work fine. 当我检索标签时,它似乎工作正常。

ASP 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# 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;
                }
            }
        }

So I need to know why it works for the label and not for the hyperlink. 因此,我需要知道为什么它适用于标签而不适用于超链接。 I also need a solution for the hyperlink. 我还需要超链接的解决方案。

You can't find your hyperlink on the server because it is not a server side control. 您在服务器上找不到超链接,因为它不是服务器端控件。 You need to add runat="server" to do just that. 您只需添加runat="server"即可。 Note the change to the href to make it valid. 注意对href所做的更改以使其生效。

<a id="hlProject" runat="server" href='<%# Eval("id", "VpnDetails.aspx?Project={0}") %>'><%# Eval("project") %></a>

You would then be able to grab it server side by finding it within your row. 然后,您可以通过在行中找到它来抓住它。

using System.Web.UI.HtmlControls;

protected void gvLastIp_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HtmlAnchor hlProject = (HtmlAnchor)e.Row.FindControl("hlProject");
    }
}

Alternatively, you could create and use an ASP.NET Hyperlink control. 或者,您可以创建和使用ASP.NET超链接控件。 This is a little more flexible/useful on the server side. 这在服务器端更加灵活/有用。

<asp:HyperLink ID="hlProject" runat="server"
  NavigateUrl='<%# Eval("id", "VpnDetails.aspx?Project={0}") %>'
  Text='<%# Eval("project") %>'>
</asp:HyperLink>
protected void gvLastIp_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink hlProject = (HyperLink)e.Row.FindControl("hlProject");
    }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink;
    }
}

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM