Response.BinaryWrite question

Discussion in 'ASP .Net' started by spmm#, Nov 3, 2004.

  1. spmm#

    spmm# Guest

    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!
     
    spmm#, Nov 3, 2004
    #1
    1. Advertising

  2. spmm#

    Hans Kesting Guest

    spmm# wrote:
    > 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
     
    Hans Kesting, Nov 3, 2004
    #2
    1. Advertising

  3. spmm#

    spmm# Guest

    > 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
    }
    }
     
    spmm#, Nov 3, 2004
    #3
  4. 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#" wrote:

    > > 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
    > }
    > }
    >
    >
    >
     
    =?Utf-8?B?ZmxldGNo?=, Nov 16, 2004
    #4
  5. spmm#

    rdevalco

    Joined:
    Mar 23, 2013
    Messages:
    1
    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
    }
     
    rdevalco, Mar 23, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DAC
    Replies:
    1
    Views:
    2,062
    Steve C. Orr, MCSD
    Jul 31, 2003
  2. =?Utf-8?B?QnJhZCBSb2JlcnRz?=

    RE: Response.BinaryWrite or something causes multiple posts

    =?Utf-8?B?QnJhZCBSb2JlcnRz?=, Aug 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    =?Utf-8?B?RGFuIEM=?=
    Aug 19, 2004
  3. Alex Nitulescu

    Response.BinaryWrite question, please

    Alex Nitulescu, Feb 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,435
    William F. Robertson, Jr.
    Feb 15, 2005
  4. Thomas Satzinger

    Response.BinaryWrite question

    Thomas Satzinger, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    Thomas Satzinger
    Jun 12, 2005
  5. Replies:
    1
    Views:
    845
Loading...

Share This Page