locked
Page_Unload Event Handler Question

    Question

  • I am trying to use the Page_Unload event (for the first time) in one of .NET Web Pages.  I added the Page_Unload event to my .NET web form (using VS .NET 2003) and set a breakpoint at the first C# instruction of the Page_Unload event and another breakpoint at the first C# instruction of the Page_Load event.

     

    When I run the page in debug mode, the Page_Unload event is being fired when the web form is loaded (right after the Page_Load event is fired).  When I exit the web, form the Page_Unload event is never fired.

    Does anybody know why???

    TIA

     

    Friday, September 01, 2006 1:53 PM

Answers

  • Hi DL,

    You probably have this code snippet in your code-behind:

    private void Page_Load(object sender, System.EventArgs e)
    {
      
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
       InitializeComponent();
       base.OnInit(e);
    }

    private void InitializeComponent()
    {
       this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    If you remove InitializeComponent(); into OnInit method then Page_Load will not be fired.

    Nevertheless, I would to use:

    protected override void OnLoad(EventArgs e)
    {
       base.OnLoad(e);
       // your code!
    }

    Good Coding!

    Javier Luna
    http://guydotnetxmlwebservices.blogspot.com/

     

    Friday, September 01, 2006 3:12 PM

All replies

  • Hi DL,

    You probably have this code snippet in your code-behind:

    private void Page_Load(object sender, System.EventArgs e)
    {
      
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
       InitializeComponent();
       base.OnInit(e);
    }

    private void InitializeComponent()
    {
       this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    If you remove InitializeComponent(); into OnInit method then Page_Load will not be fired.

    Nevertheless, I would to use:

    protected override void OnLoad(EventArgs e)
    {
       base.OnLoad(e);
       // your code!
    }

    Good Coding!

    Javier Luna
    http://guydotnetxmlwebservices.blogspot.com/

     

    Friday, September 01, 2006 3:12 PM
  • There's seem's to be confusion as to when the Page_Unload event gets fired.

    I am expecting the Page_Unload event to be fired when I navigate from my form (that has the Page_Unload event) unto another web form.

    Instead, the Page_Unload event is fired at the completion of loading the web form (that has the Page_Unload event).

    Is thje Page_Unload fired again when:

             1) there was a PostBack in the web form.

       and

             2) the user navigates to another web form.

     

     

     

    Friday, September 01, 2006 5:10 PM
  • Hi

    It's confusing for windows developer. Remember about Unload event defenition. "Occurs when the server control is unloaded from memory" after each load or post back in server side all controls Unloaded from memory. Remember we talking about web application all controls get alived by request and died when response create and send to client.So thats why after load event Unload event called.

    Friday, February 22, 2008 2:31 AM