這裏對你來說有點複雜。我有一些在GridView中列出文件的代碼,它們的大小,創建日期和修改日期等。我想向'FileName'列中添加一個HyperLink,但是我一直無法找到這樣做的方法。ASP.NET VB將HyperLink添加到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>
任何幫助,這將是極好的。
我都試過,但在<%的eval( 「文件名」)不存在。信息來自IO.FileInfo。 – user1345931 2012-04-20 08:19:41
@ user1345931:然後按照建議處理RowDataBound,並在代碼隱藏中設置Text和NavigateUrl。調試以查看會發生什麼(檢查DataRow字段)。當你從哪裏調用BindGrid? – 2012-04-20 08:22:46
確定我再次嘗試這樣做,我有以下代碼 子GridView1_RowData(BYVAL發件人爲對象,BYVALË作爲GridViewRowEventArgs) 如果e.Row.RowType = DataControlRowType.DataRow然後 HlFile.FindControl(「HlFile」) HlFile.NavigateUrl = e.Row.Cells(1).Text End If End Sub – user1345931 2012-04-20 08:25:48