<div mce_keep="true">display buttons or hyperlinks one below the other on the left side of a page each of which is associated with a separate PDF file</div>
<div mce_keep="true">display each PDF file trigged by clicking on a button or hyperlink on the SAME PAGE in a fixed area on the right-hand size of the same page as the button or hyperlink</div>
You won't have a huge amount of control over how this appears, because PDF files invoke their associated PDF Viewer on the client computer (typically Adobe Viewer). Browsers by themselves are incapable of displaying PDFs.
I have the iframe syntax working - positioning, opening the PDF viewer etc on the right-hand side of the page - but as a static rendering. Being new to Web Forms coding, I'm having difiiculty associating the iframe rendering dynamically with a button click.
Do I need to make this association in the *.aspx file or in the *.aspx.cs file? Could you give me an example of the required code?
It depends on how you are generating the list of PDF files. But a nice way would be to use Javascript and add a client-side onclick event to your buttons:
function showPDF(pdf){
document.getElementById('myIframe').src = pdf;
}
Then in your button, add onclick="showPDF('url_to_PDF_file');"
I coded the Javascript function as suggested by you but the dynamic rendering of *.pdf files is not working. The iFrame works only with the static src. Below is the relevant *.aspx file. I would be grateful if you take a look at it and tell me what I'm doing
wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
public partial class IFrames : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath("PDFs");
StringBuilder sb = new StringBuilder();
foreach (string s in Directory.GetFiles(path))
{
string pdf = Path.GetFileName(s);
sb.AppendFormat("<input type=\"button\" value=\"{0}\" onclick=\"showPDF('PDFs/{0}');\" /><br />", pdf);
}
Links.Text = sb.ToString();
}
}
Thanks for showing me for your sample that implements the iFrame control.
In my specific case, the problem of refreshing the iFrame with a new PDF file every time a button was clicked was solved by using the 'return false' in the Javascript function showPDF(pdf) in the *.aspx file:
<
script
type="text/javascript">
function showPDF(pdf)
{
document.getElementById('iFrame1').src = pdf;
return
false;
}
</
script>
and for each onclick prefix 'return' to the call to showPDF:
sankaranj
0 Points
4 Posts
How to display a PDF file on same page as a hyperlink pointing to that file
Feb 10, 2009 07:55 PM|LINK
Environment: Visual Studio 2005, ASP.NET 2.0
The task is to
Any help will be greatly appreciated.
- jps
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 10, 2009 08:22 PM|LINK
You can use an iframe, and dynamically set it's src attribute through Javascript on clicking the button or link.
http://www.quirksmode.org/js/iframe.html
You won't have a huge amount of control over how this appears, because PDF files invoke their associated PDF Viewer on the client computer (typically Adobe Viewer). Browsers by themselves are incapable of displaying PDFs.
Get rid of tags!
Web Pages CMS | My Site | Twitter
sankaranj
0 Points
4 Posts
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 11, 2009 09:43 PM|LINK
Mike,
Thanks for your prompt assistance.
I have the iframe syntax working - positioning, opening the PDF viewer etc on the right-hand side of the page - but as a static rendering. Being new to Web Forms coding, I'm having difiiculty associating the iframe rendering dynamically with a button click. Do I need to make this association in the *.aspx file or in the *.aspx.cs file? Could you give me an example of the required code?
- jps
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 11, 2009 09:55 PM|LINK
It depends on how you are generating the list of PDF files. But a nice way would be to use Javascript and add a client-side onclick event to your buttons:
function showPDF(pdf){
document.getElementById('myIframe').src = pdf;
}
Then in your button, add onclick="showPDF('url_to_PDF_file');"
Web Pages CMS | My Site | Twitter
sankaranj
0 Points
4 Posts
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 12, 2009 06:04 PM|LINK
Mike,
I coded the Javascript function as suggested by you but the dynamic rendering of *.pdf files is not working. The iFrame works only with the static src. Below is the relevant *.aspx file. I would be grateful if you take a look at it and tell me what I'm doing wrong.
Thanks,
jps
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="eIRBSelections.aspx.cs" Inherits="Home_eIRBSelections" %><%
@ Register Assembly="PHSRAG.WebControls" Namespace="PHSRAG.WebControls" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml" ><
script type="text/javascript"> function showPDF(pdf){
//alert( document.getElementById('iFrame1')); document.getElementById('iFrame1').src = pdf;}
</script><
head runat="server"> <title>eIRB Selections</title></
head><
body> <form id="form1" runat="server">
<div> <input type="submit" id="btn1" value="Electronic Signature Internal" runat="server" onclick="showPDF('http://localhost/Insight/Home/eIRB_Electronic_Signature.07.08.pdf');" /> <br /> <input type="submit" id="btn2" value="Electronic Signature External" runat="server" onclick="showPDF('http://localhost/Insight/Home/eIRB_Electronic_Signature.Outside.07.08.pdf');" style="width: 246px" /> <br /> <iframe id="iFrame1" scrolling="yes" src="http://localhost/Insight/Home/eIRB_Electronic_Signature.07.08.pdf" width="500px" height="500px" marginwidth="500px" style="left: 3.25in; width: 655px; position: absolute; top: 17px" frameborder="1" /> </div>
</form></
body></
html>moiseszarago...
Member
52 Points
62 Posts
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 12, 2009 07:07 PM|LINK
well i think that you want is to dysplay a dynamic PDF file
<iframe id="iFrame1" scrolling="yes" src=<%= FileName %> width="500px" height="500px" marginwidth="500px" style="left: 3.25in; width: 655px; position: absolute; top: 17px" frameborder="1" />
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 12, 2009 07:58 PM|LINK
I don't know the directory structure of your app, but here's a sample that has a folder called PDFs in the root of the site, containing PDF files:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IFrames.aspx.cs" Inherits="IFrames" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function showPDF(pdf) {
document.getElementById('iFrame1').src = pdf;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Links" runat="server" />
<iframe id="iFrame1"
scrolling="yes"
src="PDFs/Anchors.pdf"
width="500px"
height="500px"
marginwidth="500px"
style="left: 3.25in; width: 655px; position: absolute; top: 17px" frameborder="1" />
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
public partial class IFrames : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath("PDFs");
StringBuilder sb = new StringBuilder();
foreach (string s in Directory.GetFiles(path))
{
string pdf = Path.GetFileName(s);
sb.AppendFormat("<input type=\"button\" value=\"{0}\" onclick=\"showPDF('PDFs/{0}');\" /><br />", pdf);
}
Links.Text = sb.ToString();
}
}
Web Pages CMS | My Site | Twitter
sankaranj
0 Points
4 Posts
Re: How to display a PDF file on same page as a hyperlink pointing to that file
Feb 13, 2009 05:40 PM|LINK
Mike,
Thanks for showing me for your sample that implements the iFrame control.
In my specific case, the problem of refreshing the iFrame with a new PDF file every time a button was clicked was solved by using the 'return false' in the Javascript function showPDF(pdf) in the *.aspx file:
<
script type="text/javascript"> function showPDF(pdf){
document.getElementById('iFrame1').src = pdf;
return false;}
</
script>and for each onclick prefix 'return' to the call to showPDF:
onclick
="return showPDF(...)"- jps