Calling a Page Method when the Browser Closes

后端 未结 4 1607
[愿得一人]
[愿得一人] 2021-01-16 21:48

Hi here I\'m trying to call a [webmethod] on bodyunload method.

But it is getting fired on page load itself only. How do i prevent it?

Here\'s the code I am

4条回答
  •  悲哀的现实
    2021-01-16 22:12

    I have tested with below code and it is working fine.

    In Aspx page

    
    
    
        
    
        
    
    
    
        

    In Codebehind

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Services;
    
    public partial class ClearSessionOnPageUnload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        [WebMethod]
        public static void AbandonSession()
        {
            HttpContext.Current.Session.Abandon();
        }
    
    }
    

    You can put breakpoint at AbandonSession method to verify that it is getting hit at unload.

提交回复
热议问题