我有一个带有一些 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