如何使用条件以编程方式更改超链接文本?

时间:2014-08-12 06:28:48

标签: asp.net vb.net

我的Field页面中有aspx这个模板:

<<asp:TemplateField>
<ItemTemplate>
<%--FGRVesselID query  string of crystal report--%>
<asp:Hyperlink ID="fgr" runat="server" Target ="_blank" NavigateUrl='<%#Eval("v_id", "~/operator/printFGR.aspx?FGRVesselID={0}") %>' Text ="FGR" font-underline="false" ForeColor="blue" CssClass="links"></asp:Hyperlink>
</Itemtemplate>
<HeaderStyle CssClass ="tblheader2" />
</asp:TemplateField>

该模板字段的超链接文本应根据代码中的条件而改变。

这是背后的代码:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
   If e.Row.RowType = DataControlRowType.DataRow Then
      Dim fgr As HyperLink = DirectCast(GridView1.FindControl("fgr"), HyperLink)
      If e.Row.Cells(2).Text = "Catcher" Then
        e.Row.Cells(9).Enabled = True
      Else                       
        fgr.Text = "N/A"
      End If
   End If
 End Sub

如果cell(9)不等于“Catcher”,我必须将超链接文本更改为“N / A”,但我收到“对象引用未设置为对象实例”的错误。
    如果不满足上述条件,我将如何更改超链接文本?

1 个答案:

答案 0 :(得分:0)

要获取超链接,您需要在当前行中找到控件,而不是在整个gridview中,如下所示。

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
   If e.Row.RowType = DataControlRowType.DataRow Then
      Dim fgr As HyperLink = DirectCast(e.Row.FindControl("fgr"), HyperLink)
      If e.Row.Cells(2).Text = "Catcher" Then
        e.Row.Cells(9).Enabled = True
      Else                       
        fgr.Text = "N/A"
      End If
   End If
 End Sub