最近在做一个系统,实现的功能如下:http://community.csdn.net/Expert/TopicView1.asp?id=4445768(CSDN)
在关闭IE的时候,提示用户是否关闭IE,并向数据库写一条记录
具体代码:
HTML:
<script language="javascript" type="text/javascript">
window.onbeforeunload = ad; //语句A
//window.attachEvent('onunload',function(){<%insert();%>}); //语句B
//语句A、B任意选择一句,<%insert();%> 出问题,不能在onunload的时候执行
function ad()
{
if (document.body.offsetWidth-50 < event.clientX && event.clientY<0)
{
<%insert();%> //出问题,在onload的时候执行
return "是否退出整个管理系统吗?"
}
}
</script>
--------------------------------------------
ASP.NET
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.sqlCnnection1 = new SqlConnection("packet size=4096;user id=sa;data source=SERVER;persist security info=False;initial catalog=test");
this.sqlDataAdapter1 = new SqlDataAdapter("select * from t1",sqlCnnection1);
this.sqlCommand1 = new SqlCommand();
sqlCommand1.Connection = sqlCnnection1;
ds = new DataSet();
sqlDataAdapter1.Fill(ds);
DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();
}
------------------------------------------
protected void insert()
{
if (sqlCnnection1.State.ToString() == "Closed")
{
sqlCnnection1.Open();
}
sqlCommand1.CommandText = "insert into t1 values('04','02','02')";
//sqlCommand1.CommandText = "update t1 set name = '021' where id = '01'";
sqlCommand1.ExecuteNonQuery();
sqlCnnection1.Close();
}
当页面加载和IE关闭时请分别检查数据库的数据。。。。
加载的时候insert()执行,而关闭的时候insert()没有执行
=======================================================================
问题好象已经得到解决:
方法如下:
在页面上添加一个BUTTON,并在其中写实现的代码 insert()
最后修改部分JAVASCRIPT:<script language="javascript" type="text/javascript"> window.onbeforeunload = ad; function ad() { if (document.body.offsetWidth-50 < event.clientX && event.clientY<0) { document.getElementById("Button2").click(); } } </script>
<script language="javascript" type="text/javascript">
window.onbeforeunload = ad;
function ad()
{
if (document.body.offsetWidth-50 < event.clientX && event.clientY<0)
{
document.getElementById("Button2").click(); //更改处
}
}
</script>
说明:
<script language="javascript" type="text/javascript"> window.onbeforeunload = ad; function ad() { if (document.body.offsetWidth-50 < event.clientX && event.clientY<0) { document.getElementById("Button2").click(); } } </script>
<script language="javascript" type="text/javascript"> window.onbeforeunload = ad; function ad() { if (document.body.offsetWidth-50 < event.clientX && event.clientY<0) { document.getElementById("Button2").click(); } } </script><script language="javascript" type="text/javascript"> window.onbeforeunload = ad; function ad() { if (document.body.offsetWidth-50 < event.clientX && event.clientY<0) { document.getElementById("Button2").click(); } } </script>