我使用iframe显示aspx页面如下
<a href="#" id="trigger">this link</a>
<div id="dialog" style="display:none">
<div>
<iframe frameborder="0" height="600" width="600" src="Displaypdf.aspx"></iframe>
</div>
</div>
在Displaypdf.aspx.cs中,我显示如下pdf。我在aspx中有一个按钮,点击按钮后显示pdf(在aspx页面中,现在是iframe)
protected void Button1_Click(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
这很好用。现在我想在Page_Load做同样的事情 但是,当我将此代码放入页面加载时,这不起作用。
protected void Page_LOad(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
发生的是,我点击并获取我的iframe,但iframe是空白的,因为第一次mein pdf没有得到渲染。谁能告诉我如何解决这个问题。
答案 0 :(得分:1)
我已经尝试过你的代码了,我认为你错过了一些让它工作的东西。我已经提供了一个编辑后的代码版本,可以正常使用。
iframe页面(aspx):
<head>
<script src="../../Scripts/jquery-1.9.1.min.js"></script>
<script src="../../Scripts/jquery-ui.js"></script>
<script type="text/javascript">
var $dial1 = ""
function openlink(url, title, width, height) {
$dial1 = $('<div></div>')
.html('<iframe id="frame1" style="border: 0px; " src="' + url + '" width="100%" height="100%"></iframe>')
.dialog({
autoOpen: false,
modal: true,
height: height,
width: width,
title: title
});
$dial1.dialog('open');
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="This Link" OnClientClick="openlink('Displaypdf.aspx', 'Open', '1000', '470'); return true;" OnClick="Button1_Click" />
</form>
</body>
Displaypdf.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
希望这会有所帮助......