0

这里对你来说有点复杂。我有一些代码列出了 GridView 中的文件及其大小、创建日期和修改日期等。我想在“文件名”列中添加一个超链接,但我一直无法找到执行此操作的方法。

我在后面的代码中构建了整个 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>

对此的任何帮助都会非常好。

4

2 回答 2

1

使用 aTemplateField代替:

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

如果在 aspx 上太复杂,您可以设置NavigateUrlin :RowDataBound

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
于 2012-04-20T08:02:17.073 回答
0

将 HyperLinkField 的 NavigateUrl 属性设置为您的目标 URL。

请参阅: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}";
于 2012-04-20T08:07:10.617 回答