asp.net 离开编辑页面的提示

from911cs 2010-04-15 04:19:18
用ASP C#做一个Web系统,但是在一个添加新闻页面的时候,onload中加入了离开页面试提示的对话框,在刷新和离开页面时都有提示。但现在要命的是,我加完新闻,点击提交的时候,也出现那个该死的对话框,怎么办呢?怎么写才能跟邮箱似的,在编辑邮件的时候突然离开提示未保存,但是点击发送就不会出现那个对话框?哪位大侠帮帮忙~


写下代码:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript"><!--
var pb_strConfirmCloseMessage;
var pb_blnCloseWindow = false;
pb_strConfirmCloseMessage ="您确定要离开本页吗?";
function ConfirmClose() {
window.event.returnValue = pb_strConfirmCloseMessage;
pb_blnCloseWindow = true;
}
function ShowConfirmClose(blnValue) {
if(blnValue) {
document.body.onbeforeunload = ConfirmClose;
} else {
document.body.onbeforeunload = null;
}
}
//--></script>
</head>


<body onload="ShowConfirmClose(true);">
</body>
</html>
...全文
258 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
from911cs 2010-04-16
  • 打赏
  • 举报
回复
能不能说的具体一点了,
xshf12345 2010-04-15
  • 打赏
  • 举报
回复
当然是js里面啦
from911cs 2010-04-15
  • 打赏
  • 举报
回复
window.onbeforeunload事件在哪呢?我后台用的是asp.net(C#),前台肯定是javascript
xshf12345 2010-04-15
  • 打赏
  • 举报
回复
许多桌面应用程序会有这样的一个功能,例如word,在你修改一个文档之后,未点击保存,就直接点击关闭按钮,这时word会提示你有文档内容有改动,真的要关闭吗?

其实在web应用程序中,我们也可以模拟实现这样的功能,那就是在窗口关闭前事件(window.onbeforeunload)中加以处理:



  function GetChangeStatus(objName) {
$("#" + objName).val("true");
} //当onChange事件触发时,设置相应隐藏域的值为true,说明文本输入框里的内容有所改变了.




  var clientW, clientH;
window.onbeforeunload = function(evt) {
if ($("#SmallChanged").val() == "true") {
evt = evt || window.event;
if (window.event) {
if (evt.clientX > document.documentElement.clientWidth && evt.clientY < 0 || evt.altKey)
evt.returnValue = "Do you want to leave this page without saving?";
}
else {
if (clientH < 222)
evt.returnValue = "Do you want to leave this page without saving?";
}
}
}

document.onmousemove = function(e) {
e = e || window.event;
if (e != window.event) {
clientW = e.pageX;
clientH = e.pageY;
}
}


注意: 点击"刷新"按钮,window.onbeforeunload会也触发的,所以我们需要判断鼠标的位置是否在浏览区域的外面,即大于clientWidth和clientY小于0;另外,在firefox浏览器中要通过鼠标移动事件来获取pageX,pageY,而firefox浏览器中的pageY很特殊,没有负数(写兼容firefox的javascript极其不容易!),所以不可能小于0,所以我们只能大概规定小于某个数值的区域就是"关闭"按钮所在位置,超过这个区域的刷新动作不会触发提示.

每每听到客户抱怨自己修改和很久的数据由于自己的一个误操作而丢失时,总想让web页面也有像window窗体一样具有提示保存的功能。所以,经过一段时间的研究以后终于实现了这个功能!============ checksave.js =======================<script language="JScript">function window::onbeforeunload(){if (typeof(document.all[‘txtCheckFlag‘]) != "undefined") // to detect whether this page need prompt the save message!{ if (event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 || event.altKey || event.clientY>document.body.clientHeight) event.returnValue="If you have modified some data, you need push the save button to save them. Do you want to save?";}} test
sohu
yahoo
MTR LinkButton
======== test.aspx.vb ===============Public Class test Inherits System.Web.UI.Page Protected WithEvents txtSave As System.Web.UI.WebControls.TextBox Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents txtCheckFlag As System.Web.UI.WebControls.TextBox#Region " Web Form Designer Generated Code " ‘This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init ‘CODEGEN: This method call is required by the Web Form Designer ‘Do not modify it using the code editor. InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ‘Put user code to initialize the page here txtSave.Style.Item("display") = "none" txtCheckFlag.Style.Item("display") = "none" LinkButton1.Attributes("onclick") = "javascript:return checksave(this.href)" ‘ Page.RegisterStartupScript("Prompt", "<script language=""javascript"">function pageload() { } 0 Then Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {if (showsavemessage(‘.‘)) " + Session("URL") + ";}function pageload() {showsavemessage(‘" + Session("URL") + "‘);} 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {" + Session("URL") + ";} 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("Redirect", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}function pageload() {window.location=‘" + Session("URL") + "‘;} 0 Then Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">" + Session("URL") + " 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}function pageload() {window.location=‘" + Session("URL") + "‘;}function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧