<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="TEST1.GeneralTest.test1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/jquery-1.10.2.min.js"></script>
<script src="../Scripts/jquery-1.10.2.intellisense.js"></script>
<script src="../Scripts/bootstrap.min.js"></script>
<script type="text/javascript">
var isChange = false;
$(function () {
$("input,textarea,select").change(function () {
isChange = true;
$(this).addClass("editing");
});
$(window).bind('beforeunload', function (e) {
if (isChange || $(".editing").get().length > 0) {
return '資料尚未存檔,是否離開?';
}
})
});
//使用者正確提交表單代表是正常的離開頁面
//$("input:submit").click(function () {
// $(window).unbind('beforeunload');
//});
function setValue()
{
$("#h1").val("777");
$("#tx1").val("888");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="hidden" id="h1" runat="server" />
<input type="text" id="tx1" runat="server" />
<asp:TextBox ID="tx2" runat="server"></asp:TextBox>
<p></p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="setValue()" />
<p></p>
<a href="https://www.google.com.tw/" >link</a>
<p></p>
<input type="submit" value="送出" id="sub1" runat="server" onserverclick="sub1_ServerClick" />
</form>
</body>
</html>
--------------------------------------------------------------------------------------------
namespace TEST1.GeneralTest
{
public partial class test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
tx1.Value = "init_htmltext_123";
tx2.Text = "init_asptextbox_123";
h1.Value = "init_html_hidden_123";
}
//此處註冊送出按鈕取消驗證,一般正常送出按鈕
string script = "$('input:submit').click(function () {";
script += " $(window).unbind('beforeunload'); ";
script += " });";
Page.ClientScript.RegisterStartupScript(this.GetType(), "sss", script, true);
}
protected void Button1_Click(object sender, EventArgs e)
{
tx1.Value = "htmltext_456";
tx2.Text = "asptextbox_456";
h1.Value = "html_hidden_456";
}
protected void sub1_ServerClick(object sender, EventArgs e)
{
Response.Redirect("UseMasterPage.aspx?k1=" + tx1.Value + "&k2=" + tx2.Text + "&k3=" + h1.Value);
}
}
}