如何根据内容将NavigateUrl分配给HyperLinkField(GridView)

时间:2013-02-15 15:45:53

标签: asp.net vb.net gridview

我有一个带有一些BoundFields和一些HyperLinkFields的GridView。 HyperLinkField,我想将NavigateUrl仅分配给其中包含文本“Reject”的单元格。

这是我的MarkUp:

<asp:GridView ID="gvS25ATransactions" CellSpacing="-1" GridLines="None" AutoGenerateColumns="false" OnPreRender="gvS25ATransactions_PreRender" AllowPaging="true" AllowSorting="true" PageSize="10"  runat="server">
<Columns>
    <asp:BoundField DataField="SIN" SortExpression="SIN" HeaderText="<%$ Resources:S25ATransactions, litSIN %>" />
    <asp:BoundField DataField="TRANSACTION_TYPE_DESC_EN" SortExpression="TRANSACTION_TYPE_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
    <asp:BoundField DataField="TRANSACTION_TYPE_DESC_FR" SortExpression="TRANSACTION_TYPE_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
    <asp:BoundField DataField="DATE_UPDATED" SortExpression="DATE_UPDATED" HeaderText="<%$ Resources:S25ATransactions, litDate %>" />
    <asp:BoundField DataField="USER_CODE" SortExpression="USER_CODE" HeaderText="<%$ Resources:S25ATransactions, litUser %>" />
    <asp:HyperLinkField DataTextField="STATUS_DESC_EN" SortExpression="STATUS_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litStatus %>" />
    <asp:HyperLinkField DataTextField="STATUS_DESC_FR" SortExpression="STATUS_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litStatus %>" />
</Columns>
<PagerStyle CssClass="pagination" />

这就是我到目前为止所做的尝试:

Protected Sub gvS25ATransactions_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvS25ATransactions.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        For Each column As DataControlField In gvS25ATransactions.Columns

            If column.HeaderText = "Status" Or column.HeaderText = "Statut" Then

                For i As Integer = 0 To gvS25ATransactions.Rows.Count - 1

                    If gvS25ATransactions.Rows(i).Cells(5).Text = "Rejected by SD110" Then
                        Dim hl As HyperLink = TryCast(e.Row.FindControl("HyperLink1"), HyperLink)
                        hl.NavigateUrl = "#coucou"
                    End If

                Next

            End If

        Next

    End If

End Sub

带有“拒绝”字样的单元格将位于第5列或第6列(一个是英语,另一个是法语)。

当我执行上面的代码时,它永远不会进入for循环。

For i As Integer = 0 To gvS25ATransactions.Rows.Count - 1

它表示“i”未被宣布。由于它的保护级别,它可能无法访问

帮助?

更新

这是我为RedDevil79的建议提出的新标记

<asp:GridView ID="gvS25ATransactions" CellSpacing="-1" GridLines="None" AutoGenerateColumns="false" OnPreRender="gvS25ATransactions_PreRender" AllowPaging="true" AllowSorting="true" PageSize="10"  runat="server">
<Columns>
    <asp:BoundField DataField="SIN" SortExpression="SIN" HeaderText="<%$ Resources:S25ATransactions, litSIN %>" />
    <asp:BoundField DataField="TRANSACTION_TYPE_DESC_EN" SortExpression="TRANSACTION_TYPE_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
    <asp:BoundField DataField="TRANSACTION_TYPE_DESC_FR" SortExpression="TRANSACTION_TYPE_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
    <asp:BoundField DataField="DATE_UPDATED" SortExpression="DATE_UPDATED" HeaderText="<%$ Resources:S25ATransactions, litDate %>" />
    <asp:BoundField DataField="USER_CODE" SortExpression="USER_CODE" HeaderText="<%$ Resources:S25ATransactions, litUser %>" />
    <asp:TemplateField  SortExpression="STATUS_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litStatus %>">
        <ItemTemplate>
            <asp:HyperLink ID="STATUS_DESC_EN" runat="server"><%# Eval("STATUS_DESC_EN")%></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField  SortExpression="STATUS_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litStatus %>">
        <ItemTemplate>
            <asp:HyperLink ID="STATUS_DESC_FR" runat="server"><%# Eval("STATUS_DESC_FR")%></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>       
</Columns>
<PagerStyle CssClass="pagination" />

这是VB代码

Protected Sub gvS25ATransactions_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvS25ATransactions.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim hl As HyperLink = TryCast(e.Row.FindControl("STATUS_DESC_EN"), HyperLink)
        Dim hl2 As HyperLink = TryCast(e.Row.FindControl("STATUS_DESC_FR"), HyperLink)
        If hl.Text = "Rejected by SD110" Or hl2.Text = "Rejeté par SD110" Then
            hl.NavigateUrl = "#coucou"
            hl2.NavigateUrl = "#coucoufr"
        End If
    End If

End Sub

我现在面临的问题是hl.Text和hl2.Text总是=“”所以它永远不会进入if语句

1 个答案:

答案 0 :(得分:1)

试试这个:

将GridView中的2个超链接字段转换为模板,并将RowDataBound更改为:

If e.Row.RowType = DataControlRowType.DataRow Then

    For Each column As DataControlField In gvS25ATransactions.Columns

        If column.HeaderText = "Status" Or column.HeaderText = "Statut" Then               

                If e.Row.Cells(5).Text = "Rejected by SD110" Then
                    Dim hl As HyperLink = TryCast(e.Row.FindControl("HyperLink1"), HyperLink)
                    hl.NavigateUrl = "#coucou"
                End If

        End If

    Next

End If

说明:

您必须将超链接字段转换为模板,因为您需要FindControl函数中控件的名称。

在RowDataBound中,您不需要遍历gridview的所有行。当向其添加数据时,对gridview中的每一行执行RowDataBound。 这就是你不需要for循环的原因。

<强>更新

If e.Row.RowType = DataControlRowType.DataRow Then
     Dim hl As HyperLink = TryCast(e.Row.FindControl("STATUS_DESC_EN"), HyperLink)
     Dim hl2 As HyperLink = TryCast(e.Row.FindControl("STATUS_DESC_FR"), HyperLink)
     If hl.Text = "Rejected by SD110" Or hl2.Text = "Rejected by SD110" Then
         hl.NavigateUrl = "#coucou"
     End If
End If

您的超链接标记格式不正确:

<asp:GridView ID="gvS25ATransactions" CellSpacing="-1" GridLines="None" AutoGenerateColumns="false" OnPreRender="gvS25ATransactions_PreRender" AllowPaging="true" AllowSorting="true" PageSize="10"  runat="server">
    <Columns>
        <asp:BoundField DataField="SIN" SortExpression="SIN" HeaderText="<%$ Resources:S25ATransactions, litSIN %>" />
        <asp:BoundField DataField="TRANSACTION_TYPE_DESC_EN" SortExpression="TRANSACTION_TYPE_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
        <asp:BoundField DataField="TRANSACTION_TYPE_DESC_FR" SortExpression="TRANSACTION_TYPE_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litType %>" />
        <asp:BoundField DataField="DATE_UPDATED" SortExpression="DATE_UPDATED" HeaderText="<%$ Resources:S25ATransactions, litDate %>" />
        <asp:BoundField DataField="USER_CODE" SortExpression="USER_CODE" HeaderText="<%$ Resources:S25ATransactions, litUser %>" />
        <asp:TemplateField  SortExpression="STATUS_DESC_EN" HeaderText="<%$ Resources:S25ATransactions, litStatus %>">
            <ItemTemplate>
                <asp:HyperLink ID="STATUS_DESC_EN" runat="server" Text='<%# Eval("STATUS_DESC_EN")%>'></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField  SortExpression="STATUS_DESC_FR" HeaderText="<%$ Resources:S25ATransactions, litStatus %>">
            <ItemTemplate>
                <asp:HyperLink ID="STATUS_DESC_FR" runat="server" Text='<%# Eval("STATUS_DESC_FR")%>'></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>       
    </Columns>
    <PagerStyle CssClass="pagination" />
</asp:GridView>

您必须将字段绑定到Hyperlink的text属性。

重要提示: Text属性仅适用于“not”