我正在尝试在新标签页中打开pdf文件,并弹出一条消息说明交易已完成。但是目前它在相同的标签中打开,即chrome和firefox浏览器和microsoft edge打开了pdf。我有这个成功的消息,它没有弹出: Page.ClientScript.RegisterStartupScript(typeof(Page),“MessagePopUp”,“alert('Transaction completed successfully'); window.location.href ='LoadSheet.aspx ';“,真实); 这是我到目前为止所做的。
Derived
答案 0 :(得分:0)
您必须致电window.open('LoadSheet.aspx')
,我大部分时间都会使用它:
Page.ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Transaction completed successfully'); window.open('LoadSheet.aspx');", true);
编辑:
我的方法是调用Generic Handler(ashx)并在那里完成所有工作,通过会话变量传递数据。
VB.Net:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "myFunction", "window.open('ViewDocument.ashx');", True)
<强>更新强>
我使用Page.ClientScript.RegisterStartupScript(...)
和.ashx
Generic Handler创建了一个示例解决方案:
<强> MyPage.aspx 强>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>Show me the PDF in another tab and alert me!</div>
<asp:Button ID="btnShow" runat="server" Text="Show me!" OnClick="btnShow_Click" />
</form>
</body>
</html>
MyPage.aspx.cs(代码隐藏):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnShow_Click(object sender, EventArgs e)
{
// Pass some data to the ashx handler.
Session["myData"] = "This is my data.";
// Open PDF in new window and show alert.
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "window.open('ShowPDF.ashx'); alert('OK' );", true);
}
}
ShowPDF.ashx(Generic Handler):
<%@ WebHandler Language="C#" Class="ShowPDF" %>
using System;
using System.Web;
using System.Web.SessionState; // Added manually.
using System.IO; // Added manually.
// You'll have to add 'IReadOnlySessionState' manually.
public class ShowPDF : IHttpHandler, IReadOnlySessionState {
public void ProcessRequest (HttpContext context) {
// Do your PDF proccessing here.
context.Response.Clear();
context.Response.ContentType = "application/pdf";
string filePath = System.Web.HttpContext.Current.Server.MapPath(@"~\docs\sample.pdf");
context.Response.TransmitFile(filePath);
// Show the passed data from the code behind. It might be handy in the future to pass some parameters and not expose then on url, for database updating, etc.
context.Response.Write(context.Session["myData"].ToString());
}
public bool IsReusable {
get {
return false;
}
}
}
最终结果: (动画GIF。它会延迟3到5秒开始,因为我无法修剪它)
快速编辑:
如果您能够response
pdf的内容,那么您可以在ashx文件中执行此操作:
将sb
变量传递给ashx。在你的代码背后:
Session["sb"] = sb;
在你的经纪人处:
<%@ WebHandler Language="C#" Class="ShowPDF" %>
using System;
using System.Web;
using System.Web.SessionState; // Added manually.
using System.IO; // Added manually.
/* IMPORT YOUR PDF'S LIBRARIES HERE */
// You'll have to add 'IReadOnlySessionState' manually.
public class ShowPDF : IHttpHandler, IReadOnlySessionState {
public void ProcessRequest (HttpContext context) {
// Do your PDF proccessing here.
// Get sb from the session variable.
string sb = context.Session["sb"].ToString();
//Export HTML String as PDF.
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
我赶时间,所以这就是我所能做的。
终极编辑
修改您当前的代码:
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
//Generate Invoice (Bill) Header.
/***
ALL THE STRING BUILDER STUFF OMITED FOR BREVITY
...
***/
// Pass the sb variable to the new ASPX webform.
Session["sb"] = sb;
// Open the form in new window and show the alert.
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "window.open('NewForm.aspx'); alert('Your message here' );", true);
GridView1.AllowPaging = false;
GridView1.DataBind();
}
}
添加一个新的ASPX文件,您将在其中进行PDF处理,您不应该遇到会话和库的问题。
<强> NewForm.aspx 强>
protected void Page_Load(object sender, EventArgs e)
{
// Get sb from the session variable.
string sb = Session["sb"].ToString();
//Export HTML String as PDF.
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.End();
}