关闭浏览器时如何显示弹出窗口并保存数据 [英] how to show popup and saving the data when the browser is closed

查看:99
本文介绍了关闭浏览器时如何显示弹出窗口并保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当我关闭浏览器时,弹出窗口应确认您确定要退出吗?"如果是,则浏览器应关闭并将当前日期保存到数据库中.
我已经尝试过将代码作为

I want that when I close the browser a popup should confirm "Are you sure you want to exit?" If yes, the browser should close and current date should be saved into the database.
I have tried a code as

protected void Page_UnLoad(object sender, EventArgs e)
    {   
        DateTime d = System.DateTime.Now;
        string cn = "Data Source=hcl;Initial Catalog=mydata;Integrated Security=True";
        SqlConnection con = new SqlConnection(cn);
        SqlCommand cmd = new SqlCommand("insert into mydate values(@d)", con);
        con.Open();
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add(new SqlParameter("@d", d));
        // cmd.Parameters.Add(new SqlParameter("@b", b));
        cmd.ExecuteNonQuery();
        con.Close();

    }



但这并不成功.同样,在关闭浏览器之前,数据已保存在数据库中.我也不知道如何将警报功能称为确定要退出".请帮忙.

[将代码示例包装在预标记中,并进行编辑以清除txtspk]



but it was not succesful. Also before I close the browser the data is saved in the database. Also I don''t know how to call alert function as "Are you sure you want to exit." Please help.

[Wrapped code sample in pre tags and edited to clean up txtspk]

推荐答案

您的问题是服务器永远不知道何时关闭浏览器或何时用户离开从您的应用程序中.

浏览器知道何时会发生这种情况,因此您必须在ASP.NET代码发送到客户端的网页中的JavaScript代码中实现此功能.
Your problem is that the server never knows when the browser is closed or when the user navigates away from your application.

The BROWSER knows when this happens though, so you''ll have to implement this functionality in JavaScript code in the web page that your ASP.NET code sends to the client.


这篇关于关闭浏览器时如何显示弹出窗口并保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆