在asp.net页面中,当有验证控件,而且想在验证控件验证通过之后,
在弹出一个确认对话框,提示是否继续。
当在button按钮上添加客户端的onclick="return confirm('Are you sure to continue?')"时,验证控件的验证就会失效。
因为验证控件也是添加客户端的onclick事件。
-------------------------------------------
在asp.net 2.0, asp.net 3.5中可以使用如下方法解决。
首先将button的CausesValidation=“false”, 即
Code
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="clickme" />
其实,CausesValidation="False/True"都不会有任何影响。
接着,在后台代码的Page_Load事件中,注册客户端的onclick事件。
方法1:
Code
protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(
new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))
+ ";return (Page_IsValid && confirm('Are you sure to continue?'));";
}
/----------------
这里要特别注意的一个地方是,不能把它放在if(!isPostBack) {...}的里面,否则,第一次正常,下一次开始就会报
Microsoft JScript 运行时错误: 'WebForm_PostBackOptions' 未定义
即要求每触发一次,都要重新注册一次onclick事件。
-----------/昨天测试的时候有这个问题,可能是VS2008安装的有问题的缘故(设计模式就无法显示),
今天测试了下,可以只注册一次,即页面加载时,如下:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(
new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))
+ ";return (Page_IsValid && confirm('Are you sure to continue?'));";
}
}
方法2:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false))";
}
}
其实,方法1运行后,在客户端html中生成的onclick代码就是方法2中的代码。
若是用方法2的方法在后台直接写javascript字符串,则可以去掉最后一句,不然,就多了一次验证了。即如下:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));";
}
}
方法1每次点击按钮之前都要在page_load中注册一次,而方法2只要在page_load中注册一次就够了。
vb.net与C#生成的html客户端代码有点点不同
Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack Then
Me.Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, 'name', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));"
End If
End Sub
或则
Code
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
If Not Me.Page.IsPostBack Then
'VB.NET中要把下面这句放在Page_PreRender里,不能放在Page_Load中,且只需要注册一次就可以了
'在生成html的客户端代码中,比C#少了一部分代码。
Me.Button1.OnClientClick = Me.Page.ClientScript.GetPostBackEventReference(New System.Web.UI.PostBackOptions(Me.Button1, "", "", False, True, False, False, True, "name")) & _
";return (Page_IsValid && confirm('Are you sure to continue?'));"
End If
End Sub
-------------
asp.net 1.1里的解决方案
首先也是将button的CausesValidation=“false”, 即
Code
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="clickme" />
接着,在后台代码的Page_Load事件中,注册客户端的onclick事件。
Code
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!this.Page.IsPostBack)
{
string msg = "javascript:if (typeof(Page_ClientValidate) == 'function'){ if(Page_ClientValidate()) return window.confirm('Are you sure to continue?');}";
this.Button1.Attributes.Add("onclick",msg);
}
}
因asp.net1.1中注册onclick代码只包含javascript代码,故可以放在if(!IsPostBack){...} 里面而不会有问题。
这样就会先验证控件,通过了再弹出确认对话框。