Response.BinaryWrite question

S

spmm#

Hi!

My ASP.NET page gets a pdf-file from a SQLServer database and writes it in a
browserwindow, using Response.BinaryWrite. It basically looks like this:

public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private Byte[] GetPdf()
{
//Code to get pdf from DB
}

//etc.
}

I noticed everytime I call the page, Page_Load is executed twice... Could
someone explain this behaviour? How can I get rid of this?

Thanks!
 
H

Hans Kesting

spmm# said:
Hi!

My ASP.NET page gets a pdf-file from a SQLServer database and writes
it in a browserwindow, using Response.BinaryWrite. It basically looks
like this:
[snip]

I noticed everytime I call the page, Page_Load is executed twice...
Could someone explain this behaviour? How can I get rid of this?

Thanks!

AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting
 
S

spmm#

AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting

Where do I set AutoEventWireup? I didn't attach Page_Load twice, so that
can't be it. Below my full code... if I step through Page_Load, after
Response.End() it jumps back to byte[] myPdf = GetPdf(); instead of showing
the pdf in my browser window. Any other ideas?? Thanks.

using System;
using System.IO;

namespace WebForm1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private byte[] GetPdf()
{
//Code to get pdf from disk
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(@"C:\somefile.pdf", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
return Buffer;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
 
G

Guest

Sounds like this might be your problem:

http://www.eggheadcafe.com/forums/forumsearchbranch.asp?THREADID=14821&INTID=5

I'm running into the same issue and haven't seen a lot on the web about
this. I'm thinking about opening a problem report with MS if I don't get an
answer soon.

Steve Fletcher

spmm# said:
AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting

Where do I set AutoEventWireup? I didn't attach Page_Load twice, so that
can't be it. Below my full code... if I step through Page_Load, after
Response.End() it jumps back to byte[] myPdf = GetPdf(); instead of showing
the pdf in my browser window. Any other ideas?? Thanks.

using System;
using System.IO;

namespace WebForm1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private byte[] GetPdf()
{
//Code to get pdf from disk
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(@"C:\somefile.pdf", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
return Buffer;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
 
Joined
Mar 23, 2013
Messages
1
Reaction score
0
in Page_Load
the asp.net page life cycle does that - it appears to call it 2x...

so add :
if (Page.IsPostback == false)
{
// what you have there now
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top