0

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

这是我的标记:

<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

The problem I am facing now is that hl.Text and hl2.Text are always ="" so it never goes in the if statement

4

1 回答 1

1

Try this:

Convert the 2 hyperlink fields in GridView to template and change the RowDataBound to:

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

Explanation:

You have to convert the hyperlink fields to template because you need the name of the control in FindControl function.

In RowDataBound you don't need to iterate through all the rows of gridview. The RowDataBound is executed for every row in gridview when data is added to it. That's why you don't need the for loop.

Update:

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

Your Hyperlink tags are not well formed:

<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>

You have to Bind the field to the text property of Hyperlink.

Important: the Text property only works with ' not "

于 2013-02-19T12:37:38.980 回答