如何在asp.net中禁用验证 [英] how to disable validation in asp.net

查看:123
本文介绍了如何在asp.net中禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决asp.net中的问题.
我有一个具有四个按钮的Web应用程序,这些应用程序链接到一个Sql Server数据库,可以在其中添加,获取,更新,删除客户端.我已将所有文本框(ID,名字,姓氏,电子邮件,确认电子邮件)上的添加"按钮和更新"按钮的CausesValidation设置为true.但是,每次我检索一个客户端并尝试更新某些字段时,确认电子邮件验证都会出现RequiredFieldValidator错误需要确认电子邮件!".我的确认电子邮件包含两个验证,一个RequiredFieldValidator和一个CompareValidator.
如何禁用只弹出更新"按钮的"RequiredFieldValidator"错误?
我的数据库由ID,FirstName,LastName,Email组成.感谢

Hi, I''m having trouble solving a problem in asp.net.
I have a web application that have four buttons which is link to a Sql server database where I can Add, Get, Update, Delete clients. I have set CausesValidation to true for the Add button and the Update button on all textboxes (ID, FirstName, LastName, Email, Confirm Email). However, each time I retrieve a client and try to update some of the fields, the confirm email validation comes up with the RequiredFieldValidator error "Confirmation email required!". My Confirm Email consist of two validation, a RequiredFieldValidator and a CompareValidator.
How can I disable this RequiredFieldValidator error from popping up for the Update button only?
My database consist of ID, FirstName, LastName, Email. Thanks

推荐答案

将该控件上的Required Field Validator更改为自定义验证器.然后,您可以在后端进行验证(取决于所按下的按钮),或者更好地添加自定义JavaScript验证功能.如果然后使用页面全局标记,则该页面全局标记是通过更新按钮上的Javascript OnClientClick函数设置的.然后,您可以在自定义验证功能中检查此标志,设置时返回true,否则检查文本框(返回适当的状态).

请注意,如果需要,您仍然可以在客户端和服务器端同时进行验证.
Change your Required Field Validator on that control to a custom validator. You can then either validate on the back-end, depending on which button was pressed or, better, add a custom JavaScript validation function. If you then use a page global flag which you set using a Javascript OnClientClick function on your update button. You can then check this flag in your custom validation function, returning true when set and checking your text box if not (returning the appropriate state.)

Note that you can still validate on both client and server side if that is required.


通常,如果您需要对同一页面上的不同按钮进行不同的验证集,则最简单的解决方案是使用验证组:
http://msdn.microsoft.com/en-us/library/ms227424%28v = vs.100%29.aspx [ ^ ]

基本上,您将选择一个有意义的组名称,并将其设置为验证器控件上的ValidationGroup和应该触发该组中验证器的按钮.

但是,您需要复制适用于两个组的验证器控件.

Typically, if you need different sets of validation for different buttons on the same page, the simplest solution is to use validation groups:
http://msdn.microsoft.com/en-us/library/ms227424%28v=vs.100%29.aspx[^]

Basically, you would choose a meaningful group name, and set it as the ValidationGroup on the validator controls and the buttons which should trigger the validators in that group.

However, you would need to duplicate the validator controls which apply to both groups.

<asp:TextBox id="Email" runat="server" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email" ValidationGroup="Add" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email" ValidationGroup="Update" ... />

<asp:TextBox id="ConfirmEmail" runat="server" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmEmail" ValidationGroup="Add" ... />
<%-- NB: Not required for the "Update" group --%>

<asp:Button runat="server" Text="Add" ValidationGroup="Add" ... />
<asp:Button runat="server" Text="Update" ValidationGroup="Update" ... />


这篇关于如何在asp.net中禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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