2011-06-07 33 views
0

昨晚我刚刚注意到它,DataGrid中的ButtonField调用Vb.net作为Javascript ....用于流式PDF?

无论如何,让我们来看看这里有趣的情况。 我在DataGrid中有一个ButtonField,如果你在这里注意到它... 该ButtonField的接口看起来像一个LINK。 但是,如果我们徘徊在它上面,它显示为Javascript的调用。

Here is the Image ScreenShot

雅,这是第一种情况下。这是一个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()结果到它吗?

纠正我,如果我错了...

回答

1

这里只是一些样机,所以你明白了。我没有测试过这个。基本上你必须把上面的代码放在一个新的页面中......说出“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 

公告的 “ID = 123” 查询字符串值,我传递给receipt.aspx?

然后,您可以调用,在receipt.aspx页面这样

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

CreatePDF(id) 

...拍!刚刚意识到您正在使用Grid ...原理保持不变,只需连接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