View PDF in Atlas?

J

jdp

In my existing 1.1 app, I have a datagrid with a column that, when
clicked, displays a pdf file. In trying to convert it to 2.0 with
Atlas, the pdf will not display, no error, but no display either. The
data for the pdf is stored in SQL as binary data. I have listed the
code below. if anyone could shed some light onto the issue, I'd
greatly appreciate it.

If dr.Read Then
Dim byData(dr.GetBytes(0, 0, Nothing, 0,
Integer.MaxValue) - 1) As Byte

dr.GetBytes(0, 0, byData, 0, byData.Length)

Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.AddHeader("Content-Disposition",
"inline;filename=" & strFileName)
Response.AddHeader("Accept-Header", byData.Length)
Response.ContentType = strContentType
Response.BinaryWrite(byData)
Response.Flush()
Response.Close()
End If

Again, any feedback would be appreciated.

Thanks.
 
Joined
Oct 9, 2006
Messages
1
Reaction score
0
Ok, after searching for a few hours I found someone else asking Nikhil Kothari about almost the same problem appearing with the fileupload control. So here it comes. Due to the partial rendering of the update panel, you can not clear the response from inside the update panel. What you have to do, is go to the page and place a linkbutton outside the update panel. Actually, I am using a master page and I placed the linkbutton in there, like that:

HTML:
<asp:LinkButton ID="ALinkButton" runat="server"></asp:LinkButton>
</form>
</body>
</html>

In my case I had a usercontrol which contained a gridview and a button to export the grid in excel. So I have something like this in my usercontrol:

HTML:
<asp:GridView ID="GridView1" runat="server" … >………</asp:GridView>
<asp:Button ID="cmdExportToExcel" runat="server" Text="Export To Excel" />

So what I used to do, was to assign an event handler on cmdExportToExcel in order to response.write() the excel file which obviously, failed. In order to avoid the update panel I added the following code on my usercontrol's load event handler:

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'HACK: work around for AJAX response write
        Dim ctrl As LinkButton = Page.Master.FindControl("ALinkButton")
        If ctrl IsNot Nothing Then
            AddHandler ctrl.Click, AddressOf ExportToExcel
            Me.cmdExportToExcel.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(ctrl, ""))
        End If
    End Sub

What it does is quite straight forward, it assigns the ExportToExcel event handler (that does response.clear, response.Write etc) on the ALinkButton linkbutton located on my master page and then assigns on the onclick event the postback event of the ALinkButton.

An example of the ExportToExcel event handler would be :


Code:
Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Buffer = True
        Response.Clear()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.AddHeader("Content-Disposition", "attachment; filename=Report.xls")
 
        Me.EnableViewState = False
 
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
      'If you try to render the grid as is it will through an exception!So you place it in a form, as requested.
        Dim frm As HtmlForm = New HtmlForm()
        Me.Controls.Add(frm)
        frm.Controls.Add(Me.GridView1)
 
        frm.RenderControl(hw)
        Dim Myresponse As String = tw.ToString()
        Response.AddHeader("Content-Length", Myresponse.Length.ToString())
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
 
        Response.Write(Myresponse)
 
        Response.Flush()
        Response.End()
    End Sub

Hope I helped.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top