我怎么能确定window.onunload回传值? [英] How can i determine PostBack value in window.onunload?

查看:274
本文介绍了我怎么能确定window.onunload回传值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们要删除一些用户离开该页面后。我们正在使用window.unload事件这样做的。

In our project we are deleting something after the user left the page. We are using window.unload event for doing this.

window.onunload = function() {
  // delete something
}

我们通常使用按钮,在linkbuttons..etc的UpdatePanel所以我们没有需要检查Page.IsPostBack属性。

We are generally using buttons, linkbuttons..etc in UpdatePanel so we hadn't needed to check Page.IsPostBack property.

今天,我们意识到,我们使用了一些按钮的UpdatePanel出来的这种情况已经产生了一些错误。之后,我们决定改变我们的方法中,定义一个全局变量(变量_isPostBack =假),在顶部的我们的页面和

Today we realized that we used some buttons out of UpdatePanel and this situation had produced some errors. After that we decided to change our method, defined a global variable (var _isPostBack = false), at the top of the our page and:

window.onunload = function() {
  if (_isPostBack) {
    _isPostBack = false;
    return;
  }

  // delete something
}

Altought我设置g_isPostBack在Page_Load中,g_isPostBack没有改变。我想的RegisterClientScriptBlock,RegisterOnSubmitStatement和的RegisterStartupScript的方法。注册方法进行了onunload事件之前调用,但_isPostBack成立后onunload事件引发了......

Altought i set the g_isPostBack in Page_Load, g_isPostBack didn't change. I tried "RegisterClientScriptBlock", "RegisterOnSubmitStatement" and "RegisterStartupScript" methods. Register methods were called before the onunload event but _isPostBack was set after onunload event had triggered...

if (IsPostBack)
{
  Control c = MyClass.GetPostBackControl(this);
  bool inUpdatePanel = ControlParentForUpdatePanel(c);

  if (!inUpdatePanel)
  {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "_isPostBack = true;", true);
  }
}

有没有人帮我?

谢谢...

推荐答案

这就是诀窍...

如果您onsubmit属性添加到您的表单标签:

if you add onsubmit attribute to your form tag:

<form id="form1" onsubmit="return yourPostBack()">

和比写自己的功能:

function yourPostBack()
{
  _isPostBack = true;
  return true;
}

终于在页面加载:

and finally in the page load:

if (IsPostBack)
{
  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "_isPostBack = false;", true);
}

用这种方法你可以理解它是回发与否,window.onunload

with this method you can understand that it is postback or not, in window.onunload

这篇关于我怎么能确定window.onunload回传值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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