每每听到客户抱怨自己修改和很久的数据由于自己的一个误操作而丢失时,总想让web页面也有像window窗体一样具有提示保存的功能。所以,经过一段时间的研究以后终于实现了这个功能!============ checksave.js =======================<!-- The JavaScript is used in show a message to prompt user to save data.--><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?";}}</script><script language=javascript><!-- function checksave(arg){if (typeof(document.all[‘txtCheckFlag‘]) != "undefined") // to detect whether this page need prompt the save message! { // If need document.all["txtCheckFlag"].value=arg; document.forms(0).submit(); return false; }else // If does not need { window.location=arg; return true; }}function showsavemessage(arg){if (window.confirm("Save Change?")) { document.all["txtSave"].value="1"; document.forms(0).submit(); return false; }else { if (arg == ".") { return true; } else { window.location=arg; return false; } }}//--></script>======== test.aspx ===============<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="Test.test" smartNavigation="True" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <title>test</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> <!--#include file="checksave.js"--> </HEAD> <body MS_POSITIONING="GridLayout" onload="pageload()"> <form id="Form1" method="post" runat="server"> <a onclick="javascript:return checksave(this.href)" href=DownloadFiles1DownloadFiles1DownloadFiles1"TestNoNeedCheck.aspx">sohu</a><BR> <a onclick="javascript:return checksave(this.href)" href=DownloadFiles1DownloadFiles1DownloadFiles1"TestNoNeedCheck.aspx">yahoo</a><BR> <a onclick="javascript:return checksave(this.href)" href=DownloadFiles1DownloadFiles1DownloadFiles1"TestNoNeedCheck.aspx">MTR</a> <asp:textbox id="txtCheckFlag" style="Z-INDEX: 101; LEFT: 213px; POSITION: absolute; TOP: 91px" runat="server"></asp:textbox><asp:textbox id="txtSave" style="Z-INDEX: 102; LEFT: 211px; POSITION: absolute; TOP: 139px" runat="server"></asp:textbox><asp:linkbutton id="LinkButton1" style="Z-INDEX: 103; LEFT: 150px; POSITION: absolute; TOP: 204px" runat="server">LinkButton</asp:linkbutton> <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 238px; POSITION: absolute; TOP: 271px" runat="server" Text="Button"></asp:Button></form> </body></HTML>======== 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. <System.Diagnostics.DebuggerStepThrough()> 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() { }</script>") End Sub Private Sub txtCheckFlag_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCheckFlag.TextChanged Session("URL") = txtCheckFlag.Text If checkdata() Then ‘Session("Data") = Me.Data If InStr(txtCheckFlag.Text, "__doPostBack") > 0 Then Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {if (showsavemessage(‘.‘)) " + Session("URL") + ";}</script>") Else Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {showsavemessage(‘" + Session("URL") + "‘);}</script>") End If Else If InStr(txtCheckFlag.Text, "__doPostBack") > 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}</script>") Else If InStr(txtCheckFlag.Text, "javascript:") > 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("Redirect", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}</script>") Else Page.RegisterStartupScript("Redirect", "<script language=""javascript"">function pageload() {window.location=‘" + Session("URL") + "‘;}</script>") Response.End() End If End If End If End Sub Private Sub txtSave_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSave.TextChanged If savedata() = False Then txtSave.Text = "" txtCheckFlag.Text = "" Exit Sub End If Me.ViewState("flag") = True If InStr(txtCheckFlag.Text, "__doPostBack") > 0 Then Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">" + Session("URL") + "</script>") Else If InStr(txtCheckFlag.Text, "javascript:") > 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}</script>") Else Page.RegisterStartupScript("Redirect", "<script language=""javascript"">function pageload() {window.location=‘" + Session("URL") + "‘;}</script>") ‘Response.Redirect(Session("URL")) ‘Response.End() End If End If txtSave.Text = "" txtCheckFlag.Text = "" End Sub Private Function checkdata() As Boolean ‘check whether user really had modified the data Return True End Function Private Function savedata() As Boolean ‘do save data here! Return True End Function Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click If Me.ViewState("flag") = True Then Page.RegisterStartupScript("Redict", "<script language=""javascript"">function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}</script>") Else Page.RegisterStartupScript("Redict", "<script language=""javascript"">function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}</script>") End If End SubEnd Class
Title: 如何实现web页面的提示保存功能
Source: XML Lancer Author: Lancer
0 Comments
每每听到客户抱怨自己修改和很久的数据由于自己的一个误操作而丢失时,总想让web页面也有像window窗体一样具有提示保存的功能。
所以,经过一段时间的研究以后终于实现了这个功能!
============ checksave.js =======================
<!-- The JavaScript is used in show a message to prompt user to save data.-->
<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.\nDo you want to save?";
}
}
</script>
<script language=javascript>
<!--
function checksave(arg){
if (typeof(document.all['txtCheckFlag']) != "undefined") // to detect whether this page need prompt the save message!
{ // If need
document.all["txtCheckFlag"].value=arg;
document.forms(0).submit();
return false;
}
{
window.location=arg;
return true;
}
}
function showsavemessage(arg)
{
if (window.confirm("Save Change?"))
{
document.all["txtSave"].value="1";
document.forms(0).submit();
return false;
}
else
{
if (arg == ".")
{
return true;
}
else
{
window.location=arg;
return false;
}
}
}
//-->
</script>
剩余6页未读,继续阅读
- 科卡库尔2014-02-28不错的资源,挺好的
- donny20122013-01-29很好的资源,请试试
- 粉丝: 882
- 资源: 2万+
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助