DataGrid 中的 ButtonField 调用 Vb.net 作为 Javascript...用于流式 PDF?
我昨晚才注意到,
无论如何,让我们来看看这个有趣的案例。 我在 DataGrid 中有一个 ButtonField,如果你在这里注意到它...... 该 ButtonField 的接口看起来像一个 LINK。 但如果我们将鼠标悬停在它上面,它就会显示为 Javascript 的调用。
这是图像屏幕截图 是
的,这是第一种情况。 这是一个 javascript 的调用。 我最近没注意到这件事。 (呵呵)。
然后,如果我们点击它...它将调用createPDF()函数。幕后功能(我使用的是 VB.net)是执行这些代码;
Protected Sub createPDF()
Dim document As New Document()
Dim mem As LengthFixingStream = New LengthFixingStream()
' instantiate a iTextSharp.text.pdf.Document
'Dim mem As New MemoryStream()
' PDF data will be written here
PdfWriter.GetInstance(document, mem)
' tie a PdfWriter instance to the stream
document.Open()
Dim titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD)
document.Add(New Paragraph("Northwind Traders Receipt", titleFont))
document.Close()
' automatically closes the attached MemoryStream
Dim docData As Byte() = mem.GetBuffer()
' get the generated PDF as raw data
' write the document data to response stream and set appropriate headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=testdoc.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(docData)
Response.[End]()
End Sub
但不知何故......这当然不会将 PDF 传送到浏览器中。 因为它是由 Javascript 调用的,也不是像超链接那样直接调用(通常)。 因此,我想知道我们能否让 ASP.net 调用新窗口, 然后将 createPDF() 结果重定向到其中?
如果我错了请纠正我...
I just noticed it last night,
Anyway, let's get to the interesting case here.
I have a ButtonField within DataGrid, and if you notice it here...
The Interface of that ButtonField is looks like a LINK.
But if we hover on it, it appeared as Javascript's call.
Ya, that's the 1st case.
IT IS a javascript's call.
I didnt notice about it lately. (hehehe).
Then, if we click on that... it would call the createPDF() function. The function behind the scene (which I'm using VB.net) is to execute these code;
Protected Sub createPDF()
Dim document As New Document()
Dim mem As LengthFixingStream = New LengthFixingStream()
' instantiate a iTextSharp.text.pdf.Document
'Dim mem As New MemoryStream()
' PDF data will be written here
PdfWriter.GetInstance(document, mem)
' tie a PdfWriter instance to the stream
document.Open()
Dim titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD)
document.Add(New Paragraph("Northwind Traders Receipt", titleFont))
document.Close()
' automatically closes the attached MemoryStream
Dim docData As Byte() = mem.GetBuffer()
' get the generated PDF as raw data
' write the document data to response stream and set appropriate headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=testdoc.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(docData)
Response.[End]()
End Sub
But somehow... this of course would not deliver the PDF into the browser.
BEcause It's called by Javascript, nor the direct as Hyperlink (normally).
Thus, I'm wondering could we get the ASP.net Call new Window,
and then redirect the createPDF() result into it?
Correct me if i'm wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是一些模型,以便您了解想法。我没有测试过这个。基本上,您必须将上述代码放在一个新页面中...说“receipt.aspx”并在加载事件上执行它...您将需要设置一个 id 参数...如果数据是从db 生成pdf。
在按钮上单击添加以下内容
注意我传递给receipt.aspx 的“id=123”查询字符串值吗?
然后您可以在receipt.aspx 页面中调用它,如下所示
...拍摄!刚刚意识到您正在使用网格...原理保持不变,只需连接 RowDataBound 事件上的按钮即可。
Here is just some mockup so you get the idea. I haven't tested this. Basically you will have to put the above code in a new page...say "receipt.aspx" and execute it on the load event...you will need to setup an id parameter...if data is being pulled from the db to generate the pdf.
on the button click add the following
Notice the "id=123" querystring value I am passing to receipt.aspx?
You can then call that in the receipt.aspx page like this
...shoot! Just realized you are using a Grid...the principle remains the same, just wireup the buttons on RowDataBound event.