Link to PDF File in DataGrid

B

Bazza Formez

Hi there,

This must be simple I know...

I have an ASP.NET web page with a datagrid on it.

One of the template columns is a hyperlink that has a URL point to a PDF
document...
When the user clicks on that button.... the browser displays the pdf
document (using Adobe somehow I guess). All well & good...

The question is : How to code things such that the user may download the
pdf file instead of browsing it...... ie. such that user can save pdf file
to his/her own pc. .

How would I code the template column to achieve this ? Does this imply
using FTP in some fashion ?

Thanks !
Bazza
 
K

Ken Cox [Microsoft MVP]

Hi Bazza,

You need to send the PDF as an attachment and include the filename you want
it to use. No FTP required. I've pasted in some demo code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server" showheader="False">
<columns>
<asp:templatecolumn HeaderText="PDF">
<itemtemplate>
<asp:LinkButton runat="server"
CommandName="PDFLink" Text='<%# DataBinder.Eval(Container,
"DataItem.StringValue") %>' CommandArgument='<%# DataBinder.Eval(Container,
"DataItem.StringValue") %>' CausesValidation="false">
</asp:linkbutton>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand
' Force Open/Download prompt for a PDF
' from a template hyperlink button
' By Ken Cox
' Microsoft MVP [ASP.NET]
Dim strFilename As String
If e.CommandName = "PDFLink" Then
strFilename = e.CommandArgument
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", _
"attachment;filename=" & strFilename)
Response.WriteFile(Server.MapPath(strFilename))
Response.End()
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "mypdf" + i.ToString() & ".pdf"
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
E

Elton Wang

???????????????????????????????????????????????????????????????????????????????????>??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????>?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top