前台:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script>
function MyConfirm() {
if (confirm("确定要继续吗?") == true) {
document.getElementById("hidden1").value = "1";
}
else {
document.getElementById("hidden1").value = "0";
}
form1.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="hidden" id="hidden1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="测试Confirm"
οnclick="Button1_Click" />
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConfirmTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//此处是捕获用户操作后,根据用户操作后要执行的操作
if (this.hidden1.Value == "1")
{
this.MyGo();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//从数据库中取数据进行判断
//这里简单的改为判断页面上的textbox
if (this.TextBox1.Text == "1")
{
this.ClientScript.RegisterStartupScript(this.GetType(),"ss","<script>alert('不能添加!');</script>");
return;
}
else if (this.TextBox1.Text == "2")
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>MyConfirm();</script>");
}
else
{
MyGo();
}
}
//需要继续执行的方法
private void MyGo()
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>alert('是不是想要这个效果呢?');</script>");
hidden1.Value = "";//一定要恢复初始化
}
private void MyGo1()
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>alert('取消操作?');</script>");
hidden1.Value = "";//一定要回复初始化
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script>
function MyConfirm() {
if (confirm("确定要继续吗?") == true) {
document.getElementById("hidden1").value = "1";
}
else {
document.getElementById("hidden1").value = "0";
}
form1.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="hidden" id="hidden1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="测试Confirm"
οnclick="Button1_Click" />
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConfirmTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//此处是捕获用户操作后,根据用户操作后要执行的操作
if (this.hidden1.Value == "1")
{
this.MyGo();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//从数据库中取数据进行判断
//这里简单的改为判断页面上的textbox
if (this.TextBox1.Text == "1")
{
this.ClientScript.RegisterStartupScript(this.GetType(),"ss","<script>alert('不能添加!');</script>");
return;
}
else if (this.TextBox1.Text == "2")
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>MyConfirm();</script>");
}
else
{
MyGo();
}
}
//需要继续执行的方法
private void MyGo()
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>alert('是不是想要这个效果呢?');</script>");
hidden1.Value = "";//一定要恢复初始化
}
private void MyGo1()
{
this.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>alert('取消操作?');</script>");
hidden1.Value = "";//一定要回复初始化
}
}
}