DataGrid 中的 ButtonField 调用 Vb.net 作为 Javascript...用于流式 PDF?

发布于 2024-11-14 08:05:31 字数 1531 浏览 5 评论 0原文

我昨晚才注意到,

无论如何,让我们来看看这个有趣的案例。 我在 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.

Here is the Image ScreenShot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-11-21 08:05:31

这只是一些模型,以便您了解想法。我没有测试过这个。基本上,您必须将上述代码放在一个新页面中...说“receipt.aspx”并在加载事件上执行它...您将需要设置一个 id 参数...如果数据是从db 生成pdf。

在按钮上单击添加以下内容

Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("window.open('receipt.aspx.htm?id=123'', 'Receipt',")
sb.Append("'width=800, height=800, menubar=yes, resizable=no');<")
sb.Append("/script>")

Dim t As Type = Me.GetType()
If Not ClientScript.IsStartUpScriptRegistered(t, "PopupScript") Then
    ClientScript.RegisterStartUpScript(t, "PopupScript", sb.ToString())
End If

注意我传递给receipt.aspx 的“id=123”查询字符串值吗?

然后您可以在receipt.aspx 页面中调用它,如下所示

Dim id as String = Request.QueryString("id")

CreatePDF(id)

...拍摄!刚刚意识到您正在使用网格...原理保持不变,只需连接 RowDataBound 事件上的按钮即可。

Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim Id As String = DirectCast(e.Row.Cells(0).FindControl("quotationid"), Label).Text
    Dim myButton As New Button
    myButton = DirectCast(e.Row.Cells(4).FindControl("btnViewReceipt"), Button)
        myButton.Attributes.Add("OnClick", "window.open('receipt.aspx?id=" + id + "','Receipt','scrollbars=yes','width=800,height=800')")
    End If

End Sub

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

Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("window.open('receipt.aspx.htm?id=123'', 'Receipt',")
sb.Append("'width=800, height=800, menubar=yes, resizable=no');<")
sb.Append("/script>")

Dim t As Type = Me.GetType()
If Not ClientScript.IsStartUpScriptRegistered(t, "PopupScript") Then
    ClientScript.RegisterStartUpScript(t, "PopupScript", sb.ToString())
End If

Notice the "id=123" querystring value I am passing to receipt.aspx?

You can then call that in the receipt.aspx page like this

Dim id as String = Request.QueryString("id")

CreatePDF(id)

...shoot! Just realized you are using a Grid...the principle remains the same, just wireup the buttons on RowDataBound event.

Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim Id As String = DirectCast(e.Row.Cells(0).FindControl("quotationid"), Label).Text
    Dim myButton As New Button
    myButton = DirectCast(e.Row.Cells(4).FindControl("btnViewReceipt"), Button)
        myButton.Attributes.Add("OnClick", "window.open('receipt.aspx?id=" + id + "','Receipt','scrollbars=yes','width=800,height=800')")
    End If

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文