I have built the following in Codebehind and I want to make the 'FileName' column a Hyperlinked column, anyone have any ideas on how to do this. Here is the code that I have used to create the table.
'Create GridView headings and add titles'
Dim dt As New DataTable
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.ToString, dra.Length & " KB", dra.LastAccessTime, dra.CreationTime})
Next
GridView1.DataSource = dt
GridView1.DataBind()
Catch ex As Exception
End Try
Any help on this issue would be great, it is the last thing that I need to do and then I can get on with something more important and interesting.
Kind Regards
Tom
IT Technician
Regards
Thomas Park
Programming in ASPX and C# build Application on Microsoft .NET Framework 4.0 for Small Business and Education
Hi. right after a bit of digging I have found the following function. I'm sure I need to change it some how but I'm not quite sure what I need to add to it.
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
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
e.Row.Cells(1).Text = "<a href=""#"">" & e.Row.Cells(1).Text & "</a>"
End If
End Sub
Any ideas?
Regards
Thomas Park
Programming in ASPX and C# build Application on Microsoft .NET Framework 4.0 for Small Business and Education
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
Dim lb As New LinkButton()
lb.Text = e.Row.Cells(1).Text
lb.PostBackUrl = "#"
e.Row.Cells(1).Controls.Add(lb)
End If
End Sub
tparky
Member
112 Points
128 Posts
Table built in Codebehind need to change column type to Hyperlink
Apr 19, 2012 11:19 AM|LINK
Hi Guys,
I have built the following in Codebehind and I want to make the 'FileName' column a Hyperlinked column, anyone have any ideas on how to do this. Here is the code that I have used to create the table.
'Create GridView headings and add titles' Dim dt As New DataTable 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.ToString, dra.Length & " KB", dra.LastAccessTime, dra.CreationTime}) Next GridView1.DataSource = dt GridView1.DataBind() Catch ex As Exception End TryAny help on this issue would be great, it is the last thing that I need to do and then I can get on with something more important and interesting.
Kind Regards
Tom
IT Technician
Thomas Park
Programming in ASPX and C# build Application on Microsoft .NET Framework 4.0 for Small Business and Education
somnathmali
Contributor
2816 Points
450 Posts
Re: Table built in Codebehind need to change column type to Hyperlink
Apr 19, 2012 11:24 AM|LINK
- add one template column in GridView1 for displaying the file name and download link
- add hyperlink control here for download/preview page link
- in GridView's RowDataBound event, Find hyperlink control and assign required properties.
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
tparky
Member
112 Points
128 Posts
Re: Table built in Codebehind need to change column type to Hyperlink
Apr 19, 2012 12:30 PM|LINK
Hi Samatha,
Thank you for your post. Would it be something like this.
<asp:GridView ID="Table" runat="server" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="FullName" HeaderText="Name" SortExpression="Name" />
</Columns>
I'm not sure what I need in my code behind to get this to work.
Thomas Park
Programming in ASPX and C# build Application on Microsoft .NET Framework 4.0 for Small Business and Education
tparky
Member
112 Points
128 Posts
Re: Table built in Codebehind need to change column type to Hyperlink
Apr 20, 2012 07:30 AM|LINK
Hi. right after a bit of digging I have found the following function. I'm sure I need to change it some how but I'm not quite sure what I need to add to it.
To help I have posted my Code below
ASPX Code
<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>ASPX.VB Code
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 Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then ' Display the company name in italics. e.Row.Cells(1).Text = "<a href=""#"">" & e.Row.Cells(1).Text & "</a>" End If End SubAny ideas?
Thomas Park
Programming in ASPX and C# build Application on Microsoft .NET Framework 4.0 for Small Business and Education
Zhongqing Ta...
Star
10513 Points
1354 Posts
Re: Table built in Codebehind need to change column type to Hyperlink
Apr 23, 2012 01:32 PM|LINK
Hi,
Try this code snippet
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then ' Display the company name in italics. Dim lb As New LinkButton() lb.Text = e.Row.Cells(1).Text lb.PostBackUrl = "#" e.Row.Cells(1).Controls.Add(lb) End If End SubRegards
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework