开发者

a ButtonField within DataGrid calling Vb.net as Javascript.... for streaming PDF?

开发者 https://www.devze.com 2023-03-11 17:07 出处:网络
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...

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 iTex开发者_JAVA百科tSharp.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...


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
0

精彩评论

暂无评论...
验证码 换一张
取 消