You will need to iterate through the folder that contains your PDFs to display each one. Here's a sample that adds them to a ListBox and when you select a PDF it gets displayed.
your codes really helped me, but not for my current prblm, i used it in a different application.
the PDFs working for my web page which has a GridView in it. what i did is:
I stored all the PDF files in a hard drive and then took a copy and paste it the "C:\Documents and Settings\mhelaman\My Documents\Visual Studio 2008\Projects\MoIFacSurvey"
which is the folder which contains all the pages of my project.
problem was, if i stroed the PDFs in my Solution Explorer, then the website was getting loaded, now when i put them there in the Visual Studio 2008 folder, Will it again make my website loaded, or it is fine???
I'm not sure I completely follow you, but no matter where you store them, they will not impact your website. They will only get loaded when you select them from within your GridView, which in turn will reference the folder location you have them stored
in. If you are asking whether they will be loaded into memory when you start your website up, the answer is no. Does that answer your question?
My Blog
http://Lspence.blogspot.com
(Please MARK this post as ANSWERED, if you find it helpful)
that was what i was aksing. but good that the answer is NO.
Thanks for you support.
Dear, I got another problem.
I got a TreeView in my web page, and beside i've put a GridView, the TreeView contains Countries and Provinces (ChildNode), what i want is that by clicking any provinces (ChildNode) of the tree View, I want the GridView to show the selected Node's Details.
I mean, there should be a GridView by default, containing one of the TreeView Node's data, and then by clicking on any of the other nodes, the GridView should change the data accordingly,
Is it something like i should define SQL query for each Node? if yes, shall i write the codes under "TreeView1.SelectedNodeChanged" ? if I try this, then i am not able to define a particular node.
I'm using this snippet this way. I'm using a FileUpload control to upload files to a dir and show the uploaded files in ListBox user can double click the file name in the Listbox and open the file he uploaded & When he tries to upload a new file the code
in the Page_Load executed and the Selection in Listbox is still there Itried ClearSelection() , SelectionIndex = -1. But the item is still selected. so it won't allow me to upload a new file in there. Please tell me if I'm missing anything here. Thanks
Note: If you installed pdf plugin to your browser, it will automatically displayed.
The above code will display save dialog to save pdf even if you have a browser with pdf plugin installed. So that the user can download directly instead of displaying it in browser and then saving..
You can use HTML tag called <iframe> and src attribute as, pdf path this will display the pdf automatically, there are some attributes with URL called toolbar=no&menu=no etc. These will hide the pdf toolbar and menu options. If you want to show pdf for terms
and conditions kind, then this will be very useful.
lspence
Star
11493 Points
1707 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Jan 20, 2010 12:41 PM|LINK
You will need to iterate through the folder that contains your PDFs to display each one. Here's a sample that adds them to a ListBox and when you select a PDF it gets displayed.
<div>
<asp:Label ID="Label1" runat="server" Text="PDF Files" />
<br />
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBox1_SelectedIndexChanged" />
</div>
using System.IO; using System.Net; private string path = @"c:\PDF_Files\"; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fi = di.GetFiles(); foreach (FileInfo file in fi) { ListBox1.Items.Add(file.Name); } } } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { WebClient client = new WebClient(); Byte[] buffer = client.DownloadData(path + ListBox1.SelectedValue); Response.ContentType = "application/pdf"; Response.AddHeader("content-length", buffer.Length.ToString()); Response.BinaryWrite(buffer); }http://Lspence.blogspot.com
(Please MARK this post as ANSWERED, if you find it helpful)
RMhelaman
Member
6 Points
5 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Jan 21, 2010 03:52 AM|LINK
Thanks so much friend!
your codes really helped me, but not for my current prblm, i used it in a different application.
the PDFs working for my web page which has a GridView in it. what i did is:
I stored all the PDF files in a hard drive and then took a copy and paste it the "C:\Documents and Settings\mhelaman\My Documents\Visual Studio 2008\Projects\MoIFacSurvey"
which is the folder which contains all the pages of my project.
problem was, if i stroed the PDFs in my Solution Explorer, then the website was getting loaded, now when i put them there in the Visual Studio 2008 folder, Will it again make my website loaded, or it is fine???
Thats my question, hope you can answer...
Thanks
lspence
Star
11493 Points
1707 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Jan 21, 2010 12:54 PM|LINK
I'm not sure I completely follow you, but no matter where you store them, they will not impact your website. They will only get loaded when you select them from within your GridView, which in turn will reference the folder location you have them stored in. If you are asking whether they will be loaded into memory when you start your website up, the answer is no. Does that answer your question?
http://Lspence.blogspot.com
(Please MARK this post as ANSWERED, if you find it helpful)
RMhelaman
Member
6 Points
5 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Jan 24, 2010 05:14 AM|LINK
Exactly Ispence!
that was what i was aksing. but good that the answer is NO.
Thanks for you support.
Dear, I got another problem.
I got a TreeView in my web page, and beside i've put a GridView, the TreeView contains Countries and Provinces (ChildNode), what i want is that by clicking any provinces (ChildNode) of the tree View, I want the GridView to show the selected Node's Details.
I mean, there should be a GridView by default, containing one of the TreeView Node's data, and then by clicking on any of the other nodes, the GridView should change the data accordingly,
Is it something like i should define SQL query for each Node? if yes, shall i write the codes under "TreeView1.SelectedNodeChanged" ? if I try this, then i am not able to define a particular node.
deedee1969
Member
24 Points
14 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Mar 10, 2010 07:19 PM|LINK
Thank you, Thank U THANK U !!!
This worked perfectly. But now how do I force the user to close the PDF before going back to do something else in the webpage?
I am getting greedy now.
BTW, I am using VB.NET not C#
surena
Member
2 Points
1 Post
Re: Open PDF file on button click or hyperlink from asp.net
May 07, 2010 07:52 AM|LINK
Hi...
The problem scenerio is that i have some doc files residing at "F:\\test\\*".
I want to list out and open doc files from my asp.net application on hyperlink click, language is C#.
I went through your pdf example but it results into error
An invalid character was found in text content. Line 1, Position 1
?ࡱ? ^Please help...mail me at sonusneh@gmail.comashwoody
Member
120 Points
36 Posts
Re: Open PDF file on button click or hyperlink from asp.net
May 07, 2010 08:35 AM|LINK
This article should get you started:
http://aspnet.4guysfromrolla.com/articles/102109-1.aspx
Although it is in VB, you should be able to convert easy enough
AbidCharlott...
Member
24 Points
25 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Jun 21, 2010 08:01 PM|LINK
Hey Ispence,
I'm using this snippet this way. I'm using a FileUpload control to upload files to a dir and show the uploaded files in ListBox user can double click the file name in the Listbox and open the file he uploaded & When he tries to upload a new file the code in the Page_Load executed and the Selection in Listbox is still there Itried ClearSelection() , SelectionIndex = -1. But the item is still selected. so it won't allow me to upload a new file in there. Please tell me if I'm missing anything here. Thanks
01.if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "click") 02. { 03. int idx = LstBxUploadedFiles.SelectedIndex; 04. ListItem item = LstBxUploadedFiles.SelectedItem; 05. 06. DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["DocumentsLocation"] + "\\" + (string)Session["CreatedFolderName"]); 07. FileInfo[] files = di.GetFiles(); 08. foreach (FileInfo fi in files) 09. { 10. if (fi.Name == item.Text) 11. { 12. Response.ClearContent(); 13. Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name); 14. Response.AddHeader("Content-Length", fi.Length.ToString()); 15. string CId = Request["__EVENTTARGET"]; 16. LstBxUploadedFiles.ClearSelection(); 17. Response.TransmitFile(fi.FullName); 18. Response.End(); 19. 20. 21. } 22. } 23. 24. 25. } 26. LstBxUploadedFiles.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(LstBxUploadedFiles, "click")); 27. LstBxUploadedFiles.ClearSelection();rafemuhammed
Member
302 Points
97 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Aug 07, 2010 01:04 PM|LINK
</div><div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;" mce_style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;">Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment; filename=testpdf.pdf");
Response.TransmitFile( Server.MapPath("~/testpdf.pdf") );
Response.End();</div>
Note: If you installed pdf plugin to your browser, it will automatically displayed.
The above code will display save dialog to save pdf even if you have a browser with pdf plugin installed. So that the user can download directly instead of displaying it in browser and then saving..
Best of luck
rafemuhammed
MCA
rafes.net
nagappmking
Member
6 Points
3 Posts
Re: Open PDF file on button click or hyperlink from asp.net
Sep 02, 2010 03:17 PM|LINK
You can use HTML tag called <iframe> and src attribute as, pdf path this will display the pdf automatically, there are some attributes with URL called toolbar=no&menu=no etc. These will hide the pdf toolbar and menu options. If you want to show pdf for terms and conditions kind, then this will be very useful.
Thanks.
Digitally Signed,
Nagaraj
+919741012395