0

我正在尝试在超链接字段中添加导航 url。

我的 gridview 上有 5 列,最后一个是

  <asp:HyperLinkField DataNavigateUrlFields="vID" 
            DataNavigateUrlFormatString="Page2.aspx?field={0}" HeaderText="send" 
            Text="send"></asp:HyperLinkField>

以及后面要添加的代码

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    Dim hk As HyperLink = DirectCast(e.Row.Cells(4).Controls(0), HyperLink)
    hk.NavigateUrl += "&TN=table1"

End Sub

我昨天让它工作了,但一定是不小心删除了代码,在这里我找不到哪里出错了,我收到一个错误“指定的参数超出了有效值的范围。参数名称:索引”

4

1 回答 1

1

您必须检查RowType,否则您HyperLink还要在标题中寻找 。

VB

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs )
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim link = DirectCast(e.Row.Cells(4).Controls(0), HyperLink)
        link.NavigateUrl &= "&TN=table1"
    End If
End Sub

C#

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink link = (HyperLink)e.Row.Cells[4].Controls[0];
        link.NavigateUrl += "&TN=table1";
    {
}
于 2012-04-18T22:48:00.700 回答