Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am preparing a webpage in asp.net using c#

I want certain code to be executed on page load and some other code to be executed on page unload. My code is as follows:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //code to be executed on page load when user enter the page           
        }
   }

// Other code for execution depending on user requirement 

protected override void OnUnload(EventArgs e)
    {
            base.OnUnload(e);
        
            // code to be executed on user leaves the page    
   }


My problem is that the code for unload gets fired on pageload itself whereas I expect only pageload code to fire at page load and pageunload code to fire only when user leaves the page.

Kindly help me by advising what I am missing in the code

Many thanks for help
Posted

With AutoEventWireup which is turned on by default on a page you can just add methods prepended with Page_event and have ASP.NET connect to the events for you.

In the case of Unload the method signature is:

C#
protected void Page_Unload(object sender, EventArgs e)


So you need to write:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //code to be executed on page load when user enter the page           
        }
   }
 
// Other code for execution depending on user requirement 

protected void Page_UnLoad(object sender, EventArgs e)
    {
            // code to be executed on user leaves the page    
   }
 
Share this answer
 
v2
Comments
Member 10235977 15-Feb-15 6:17am    
Thanks Sir but it has still not worked for me as Page_UnLoad code still fires on page load itself. I wonder if the code page_UnLoad has to be nested in some command like if(!IsPostBack) as done in Page_Load method. I have tried nesting in if(!IsPostBack) and also if(!IsCallBack) but it does not seem to work. Apparently I need a little more elaboration. Kindly help.
Anisuzzaman Sumon 15-Feb-15 6:25am    
Show More code or You may use flag in if(!IsPostBack){
flag=true;
}

And then check the flag before executing the code inside Page_UnLoad . like protected void Page_UnLoad(object sender,EventAgrs e){ if(!flag){ //code to be executed on user leaves the page} }
Member 10235977 15-Feb-15 8:14am    
My code for page load is:

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
using (SqlConnection con = Connections.GetConnection())
{
SqlCommand com = new SqlCommand("UPDATE OnOffStatus SET Status=@Status", con);
com.Parameters.AddWithValue("@Status", "on");
com.ExecuteNonQuery();
con.Close();
}
}
}

And my Page UnLoad code is

protected void Page_UnLoad(object sender, EventArgs e)
{

using (SqlConnection con = Connections.GetConnection())
{
SqlCommand com = new SqlCommand("UPDATE OnOffStatus SET Status=@Status", con);
com.Parameters.AddWithValue("@Status", "off");
con.Close();
}

}

I have not been able to use 'flag= true', as I get the error that the name 'flag' does not exist in the current context.

I shall be highly grateful for your help for your help in solving this issue.
Actually Page_UnLoad event is rasied when the page is rendered and loaded completely at the end of its page life cycle.It is not just like you have done through form_closing event in windows form application.Instead you can detect browser close event and then call server side event using ajax.As shown like below


JavaScript
<script type="text/javascript">
$(function () {
    $(window).unload(function () {

   jQuery.ajax({ url: "http://localhost:49591/yourwebform.aspx", async: false });
      });
});
</script>


If you have not used it before just ask google.Happy coding!:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900