ASP.NET AJAX:更新面板中的按钮会导致在window.onbeforeunload之前激发OnClick事件
ASP.NET AJAX: button inside an updatepanel causes the OnClick event to fire before window.onbeforeunload
aspx:
<script type="text/javascript">
window.onbeforeunload = function (e) {
if (unsavedData()) {
return "Exit page?";
}
}
function unsavedData() {
//logic for unsaved data here
//let's just return true for this example
return true;
}
</script>
<asp:UpdatePanel ID="updatePnlContent" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
<%-- triggers for other buttons --%>
</Triggers>
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click"/>
<%-- Other fields and buttons --%>
</ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
背后的代码
protected void btnSave_Click(object sender, EventArgs e)
{
//save data
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
如果我单击"取消"按钮,并且有未保存的数据,我会得到一个提示,如预期的那样,单击"确定"继续,或点击"取消"停留在此页面上。然而,当我按Cancel停留在这个页面上时,我在一个Microsoft AJAX javascript中得到了一个未指定的错误。
我认为发生的事情是在window.onbeforeunload函数之前触发了OnCilck事件。如何确保只有当用户单击"确定"继续时才触发OnClick事件,而不是当单击"取消"留在该页面时才触发?
从Cancel按钮中删除OnClick方法,并将其替换为OnClieckClick,然后调用unsavedData()函数。
如果你想使用OnClick和OnClientClick,请参阅这篇文章了解解决方案:OnClientClick和On Click不同时工作?
相关文章:
- onbeforeunload和asp:Safari和FireFox中的更新面板
- 从window.onbeforeunload调用方法背后的代码
- 如何显示window.onbeforeunload警报的引导模式
- onbeforeunload未正确触发
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- confirm() on window.onbeforeunload
- JavaScript onbeforeunload()函数未提交表单
- 检查onbeforeunload中的ace编辑器,查看是否进行了更改
- Mysql and onbeforeunload
- 如何在提示下获取和使用从window.onbeforeunload事件的返回语句显示的值
- 如果我在 window.onbeforeunload 事件上确认是,我如何调用另一个 Java 脚本函数
- window.onbeforeunload在IE8中不触发(在Firefox,Chrome和Safari中工作正常)
- onbeforeunload导航使用location.href
- 使用onbeforeunload绑定事件
- 如何禁用“警告”;onbeforeunload”;当“;重新加载”;同一页
- onbeforeunload确认对话框可以用设计良好的自定义对话框进行自定义
- 如何对onbeforeunload中的子事件作出反应
- Onbeforeunload-windows分配不'不适用于Chrome
- PHP-window.onbeforeunload和自动注销冲突
- 使用apachewicket的onbeforeunload事件