解决方案:
给按钮添加Attributes属性,即Button1.Attributes["OnClick"] = "return confirm('are you sure?')";
这样在客户端生成 OnClick="return confirm('are you sure?')" 用户执行按钮的操作时,先在本地执行弹出一个confirm的确认窗口,再根据用户的选择,判断是否要执行按钮的操作。可能在刚开始的时候会认为服务器端是怎么知道用户的选择,其实在点击后,当选择“取消”时客户端自己进行确认,并没有发到服务器端进行确认。
下面就是一个例子
.aspx代码
<
form id
=
"
Form1
"
method
=
"
post
"
runat
=
"
server
"
>
< FONT face = " 宋体 " >
< asp:Button id = " Button1 " runat = " server " Text = " Button " ></ asp:Button >
< asp:Label id = " Label1 " runat = " server " > Label </ asp:Label ></ FONT >
</ form >
< FONT face = " 宋体 " >
< asp:Button id = " Button1 " runat = " server " Text = " Button " ></ asp:Button >
< asp:Label id = " Label1 " runat = " server " > Label </ asp:Label ></ FONT >
</ form >
.cs代码
private
void
Page_Load(
object
sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Button1.Attributes["OnClick"] = "return confirm('are you sure?')";
Label1.Text="are you sure";
}
private void Button1_Click( object sender, System.EventArgs e)
{
Label1.Text="I'm sure";
}
}
{
// 在此处放置用户代码以初始化页面
Button1.Attributes["OnClick"] = "return confirm('are you sure?')";
Label1.Text="are you sure";
}
private void Button1_Click( object sender, System.EventArgs e)
{
Label1.Text="I'm sure";
}
}