By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

How to link to text files in datagrid?

P: n/a
hi,

I have a datagrid in my asp.net application, I am using vb.net
language.
I want to have my last column as a link column, link to text file.
The link path is "c:\Temp\" & F_File_Name, which is the 2nd
column.

For example, the data grid should like this:

Batch # File Name File Status File link
1 EF010405.txt Rows entered Link (when user click it, it should
link to c:\temp\EF010405.txt)

I searched the web and didn't find any sample code for file linkage
throw hyperlink column in a datagrid, can someone show me how to do
it?

Thank you.

Here is the Html:
-----------------------------------------------
<asp:datagrid id="dgFundStatus" Runat="server" CellPadding="3"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundColumn DataField="F_Batch_Num" HeaderText="Batch
#"></asp:BoundColumn>
<asp:BoundColumn DataField="F_File_Name" HeaderText="File
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="File_Status" HeaderText="File
Status"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="File link"
DataNavigateUrlField="FilePath"
DataNavigateUrlFormatString="{0}" DataTextField="File link"
ItemStyle-CssClass="testStyle">
</asp:HyperLinkColumn>
</Columns>
</asp:datagrid>

Here is the code:
------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim strSql As New SqlCommand
Dim dtrDataGrid As SqlDataReader
Dim dataAdapter As New SqlDataAdapter
Dim dataSet As New DataSet
Dim FilePath As String
Try
If Not IsPostBack Then
conEZDb.Open()
strSql.CommandText = "SELECT F.F_Batch_Num,
F.F_File_Name, F.F_Transmit_Date, S.File_Status_Description FROM
Epay_Fund_Batch_Table F INNER JOIN Epay_File_Status S ON
F.F_File_Status = S.File_Status "
strSql.Connection = conEZDb
strSql.CommandType = CommandType.Text
dataAdapter.SelectCommand = strSql
dataAdapter.Fill(dataSet, "Funding")
Dim FundingInfo As DataTable =
dataSet.Tables("Funding")
Dim dvFundingInfo As New
DataView(dataSet.Tables("Funding"))
Session("FundingSession") = dvFundingInfo
dvFundingInfo.Sort = strViewString
dgFundStatus.DataSource = dvFundingInfo

dgFundStatus.DataBind()
conEZDb.Close()
End If
Catch err As Exception
End Try
End Sub

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jen:

This approach doesn't work if the browser is not on the same machine as the
server. The user's browser will look to the local C: drive for the file.
You'll want to have the text files underneath the web application (or somewhere
else under a website where IIS can server them from) and put in a link or
relative path to the file.

Make sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/
hi,

I have a datagrid in my asp.net application, I am using vb.net
language.
I want to have my last column as a link column, link to text file.
The link path is "c:\Temp\" & F_File_Name, which is the 2nd
column.
For example, the data grid should like this:

Batch # File Name File Status File link
1 EF010405.txt Rows entered Link (when user click it, it should
link to c:\temp\EF010405.txt)
I searched the web and didn't find any sample code for file linkage
throw hyperlink column in a datagrid, can someone show me how to do
it?

Thank you.

Here is the Html:
-----------------------------------------------
<asp:datagrid id="dgFundStatus" Runat="server" CellPadding="3"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundColumn DataField="F_Batch_Num" HeaderText="Batch
#"></asp:BoundColumn>
<asp:BoundColumn DataField="F_File_Name" HeaderText="File
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="File_Status" HeaderText="File
Status"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="File link"
DataNavigateUrlField="FilePath"
DataNavigateUrlFormatString="{0}" DataTextField="File link"
ItemStyle-CssClass="testStyle">
</asp:HyperLinkColumn>
</Columns>
</asp:datagrid>
Here is the code:
------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
Dim strSql As New SqlCommand
Dim dtrDataGrid As SqlDataReader
Dim dataAdapter As New SqlDataAdapter
Dim dataSet As New DataSet
Dim FilePath As String
Try
If Not IsPostBack Then
conEZDb.Open()
strSql.CommandText = "SELECT F.F_Batch_Num,
F.F_File_Name, F.F_Transmit_Date, S.File_Status_Description FROM
Epay_Fund_Batch_Table F INNER JOIN Epay_File_Status S ON
F.F_File_Status = S.File_Status "
strSql.Connection = conEZDb
strSql.CommandType = CommandType.Text
dataAdapter.SelectCommand = strSql
dataAdapter.Fill(dataSet, "Funding")
Dim FundingInfo As DataTable =
dataSet.Tables("Funding")
Dim dvFundingInfo As New
DataView(dataSet.Tables("Funding"))
Session("FundingSession") = dvFundingInfo
dvFundingInfo.Sort = strViewString
dgFundStatus.DataSource = dvFundingInfo
dgFundStatus.DataBind()
conEZDb.Close()
End If
Catch err As Exception
End Try
End Sub
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*

Nov 19 '05 #2

P: n/a
The reason I put C:\ is because we haven't find the appropreate place,
it's a testing path.

My question is I don't know how to put the path on this code and make
it work.

Can you show me how ?

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.