ASP.NET验证控件和Javascript确认框 [英] ASP.NET Validation Controls and Javascript Confirm boxes

查看:110
本文介绍了ASP.NET验证控件和Javascript确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用.NETs服务器端输入验证控件的页面。本页面也有一个JavaScript确认框,火灾时,提交表单。目前,当选择提交按钮时,JavaScript确认框出现,一旦确认了ASP.NET服务器端验证控件被解雇。我想火的服务器端验证控件之前显示的JavaScript确认框。

如何实现这一点?香港专业教育学院包括我目前的code以下的样本。

sample.aspx

 < ASP:文本框的id = foo的RUNAT =服务器/>
< ASP:使用RequiredFieldValidator ID = VAL RUNAT =服务器的ControlToValidate =富/>
< ASP:按钮的ID =提交RUNAT =服务器的OnClientClick =收益确认(确认提交?')/>
 

sample.aspx.vb

 小组的Page_Load()
    如果Page.IsPostback(),然后
        Page.Validate()

        如果Page.IsValid然后,
            过程页面在这里
        结束如果
    结束如果
结束小组
 

感谢您的帮助。

解决方案

这似乎是一个很普遍的问题。

解决方法:

首先确认页面,然后调用确认,如<一个href="http://www.$c$cdigest.com/$c$cDigest/73-Fire-Validator-Controls-Before-JavaScript-Confirm-Box-Fires-in-ASP-Net-Page.aspx">here和这里。 这确实有调用验证两次的缺点 - 在code一次,一旦产生code在提交的onclick

如何使这项工作正常,即第一个(也是唯一一次)验证页面,然后显示确认中,我还不知道。

编辑:这里的'SA有益的建议:

  

ASP.NET做幕后是什么   当验证控件存在,就是添加   自动生成的onclick事件   每个按钮。这OnClick事件会   取代了自定义的onclick事件。因此,要   克服这个我做了以下内容:

     
      
  1. 添加的CausesValidation =假
  2.   
  3. 添加验证()和IsValid的code到onClick事件   后面的页面现在模拟   缺少自动生成验证code   。后面的按钮
  4.   

编辑2:一个完整​​的例子

 &LT; ASP:按钮的ID =btnSubmit按钮=服务器文本=提交的OnClientClick =如果(Page_ClientValidate()){返回确认('你想提交本页面')}的CausesValidation =FALSE/&GT?;
 

I have a page using .NETs server-side input validation controls. This page also has a javascript confirm box that fires when the form is submitted. Currently when the Submit button is selected, the javascript confirm box appears, and once confirmed the ASP.NET server-side validation controls are fired. I would like to fire the server-side validation controls BEFORE the javascript confirm box is displayed.

How can this be accomplished? Ive included a sample of my current code below.

sample.aspx

<asp:textbox id=foo runat=server />
<asp:requiredfieldvalidator id=val runat=server controltovalidate=foo />
<asp:button id=submit runat=server onClientClick=return confirm('Confirm this submission?') />

sample.aspx.vb

Sub Page_Load()
    If Page.IsPostback() Then
        Page.Validate()

        If Page.IsValid Then
            'process page here'
        End If
    End If
End Sub

Thanks for any help.

解决方案

This seems to be a very common problem.

The workaround:

Validate the page first, then call confirm, as shown here and here. This does have the drawback of calling the validation twice - once in your code, and once in the generated code in the submit onclick.

How to make this work properly, i.e. Validate the page first (and only once), then show the confirm box, I do not yet know.

Edit: Here's a useful suggestion:

What ASP.NET does behind the scenes when validation controls exist, is add an autogenerated onClick event for each button. This OnClick event would supercede the custom OnClick event. So to overcome this I did the following:

  1. add CausesValidation = False
  2. added Validate() and IsValid code to the onClick event behind the page to simulate the now missing autogenerated validation code behind the button.

Edit 2: A complete example

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="if (Page_ClientValidate()){ return confirm('Do you want to submit this page?')}" CausesValidation="false" />

这篇关于ASP.NET验证控件和Javascript确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆