我创建了一个PDF,我试图在新窗口中打开它(或新标签,但我现在工作的新窗口)打开PDF在新页面
这里是我的aspx代码。链接按钮位于面板中的GridView中,该面板位于更新面板中(是的,我看到有人遇到更新面板和此问题,但我必须这样做)。
<ItemTemplate>
<asp:LinkButton ID="ReportLink" runat="server" CommandName="GenReport"
CommandArgument='<%# Container.DataItemIndex %>'
OnClientClick="LoadPDF()" PostBackUrl="~/Indices/myPDF.aspx"
Text="View Report" ToolTip="Genereate a PDF of the Report"></asp:LinkButton>
</ItemTemplate>
这里是我的javascript打开的新窗口
function LoadPDF() {
window.open('myPDF.aspx', '',
'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
}
这里是C#。第一个片段是点击链接按钮时调用的内容(在javascript之后)。第二种是实际创建PDF并尝试将其发送到新窗口的方法。
{
DataTable dt = (DataTable)Session["IndexTable"]; //copy of the GirdView
int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
DataRow row = dt.Rows[rowIndex];
GenerateReport(row["ITEM_ID"].ToString());
}
private void GenerateReport(string itemID)
{
OracleConnection connection = new OracleConnection(connstr);
try
{
connection.Open();
//code here omitted, all sql and setting up info for the doc
// giving TurnOverData a dataset
ReportDocument doc = new ReportDocument();
doc.FileName = Server.MapPath("myRpt.rpt");
doc.SetDataSource(TurnoverData);
//here adding parameters to doc
System.IO.MemoryStream stream = (System.IO.MemoryStream)doc.ExportToStream(ExportFormatType.PortableDocFormat);//leftover code from previous functionality (i dont think it is doing anything)
BinaryReader Bin = new BinaryReader(doc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="application/pdf";
Response.BinaryWrite(Bin.ReadBytes(Convert.ToInt32(Bin.BaseStream.Length)));
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
Logger.logError("GenerateReport() " + ex.Message);
Utilities.SendAnAlert(this, "An error occurred while generating PDF file and the file could not be displayed.");
}
finally
{
connection.Close();
connection.Dispose();
}
}
在此先感谢
UPDATE:
后通过我们的旧代码多挖我发现周围的工作。几乎所有你们给我的代码都已经在我们的代码中,我只需要弄清楚如何使用我们的代码。我结束了使用我的LoadPDF函数,但不得不传递一个字符串参数,因为URL使用“?=”,我必须填写。我感谢所有的帮助!
看来这个问题是产生实际的PDF文件,而不是开放的浏览器窗口(并没有提到阿贾克斯后btw)。 – BluesRockAddict 2012-04-18 21:06:52
@BluesRockAddict谢谢,我已经更新了我的答案。 ;) – mayhewr 2012-04-18 21:11:02
我也试过这个,它打开新选项卡很好,但我的PDF仍未显示。我认为我的主要问题是让我的PDF显示。 – 2012-04-19 17:23:43