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. Advertisements

  2. spmm#

    Hans Kesting 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
     
    Hans Kesting, Nov 3, 2004
    #2
    1. Advertisements

  3. spmm#

    spmm# Guest

    AutoEventWireup set to true? Check the "Web Form Designer Generated code"
    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. spmm#

    Guest 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

     
    Guest, Nov 16, 2004
    #4
  5. spmm#

    rdevalco

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

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 (here). After that, you can post your question and our members will help you out.