ASP.NET VB将HyperLink添加到GridView

时间:2012-04-20 07:48:04

标签: asp.net vb.net

这对你来说有点复杂。我有一些代码在GridView中列出文件的大小,创建日期和修改日期等。我想在'FileName'列中添加HyperLink,但我一直无法找到这样做的方法。

我在后面的代码中构建了整个GridView。

 Sub BindGrid(path)
    'Create GridView headings and add titles' 
    Dim dt As New DataTable
    Dim Name As New HyperLinkField

    dt.Columns.Add("FileName")
    dt.Columns.Add("Size")
    dt.Columns.Add("Last Accessed")
    dt.Columns.Add("Created Date")


    Dim Username = User.Identity.Name.ToString
    Dim TrimUsername = Username.Replace("ASHDOWN\", "")
    'Dim Path As String = "~\\Users\\" & TrimUsername''
    LoginNameDisplay.Text = TrimUsername

    Dim di As New IO.DirectoryInfo(Server.MapPath(Path))
    Try
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        'list the names of all files in the specified directory' 
        For Each dra In diar1

            dt.Rows.Add(New Object() {dra.Name, dra.Length & " KB", dra.LastAccessTime, dra.CreationTime})
        Next
        GridView1.DataSource = dt
        GridView1.DataBind()
    Catch ex As Exception

    End Try




End Sub

这是我的ASPX页码

   <asp:GridView ID="GridView1" Width="100%" runat="server" CellPadding="4" 
        EnableModelValidation="True" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:HyperLinkField AccessibleHeaderText="Hyperlink" />
                </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>

对此的任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

改为使用TemplateField

<asp:TemplateField>
<ItemTemplate>
    <asp:HyperLink ID="HlFile" runat="server"
        Text='<%# Eval("FileName") %>'>
    </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

如果在aspx上过于复杂,您可以在RowDataBound中设置NavigateUrl

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
         DataRow row = ((DataRowView)e.Row.DataItem).Row;
         HyperLink hlFile = (HyperLink)e.Row.FindControl("HlFile");
         // set the hyperlink url just as you please
         hlFile.NavigateUrl = row.Field<String>("URL");
    }
}

编辑:这是VB版本:

Protected Sub GridView1_RowDataBound(sender As [Object], e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim row As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row
        Dim hlFile = DirectCast(e.Row.FindControl("HlFile"), HyperLink)
        ' set the hyperlink url just as you please
        hlFile.NavigateUrl = row.Field(Of String)("URL")
    End If
End Sub

答案 1 :(得分:0)

将HyperLinkField的NavigateUrl属性设置为目标网址。

请参阅:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.navigateurl.aspx#Y228

您可以使用DataNavigateUrlFormatString设置属性,类似于以下内容:

HyperLinkField hlf = YourGridView.Columns["hyperlinkColumnName"] as HyperLinkField;
hlf.DataNavigateUrlFormatString = "~\details.aspx?ProductID={0}";