创建使用ASP.NET验证控件自定义响应 [英] Creating a custom response with ASP.NET validation controls

查看:91
本文介绍了创建使用ASP.NET验证控件自定义响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证各种ASP.NET控件,但不是显示旁边的标准文本/星/图像中的每个验证失败时,我需要显示自定义内容(改变输入文本框的边框颜色,显示工具提示等)。我可以用在大多数情况下,一个标准的验证控件(例如,为的RequiredFieldValidator一个TextBox),除了显示验证失败。

I need to validate various ASP.NET controls, but instead of displaying the standard text/asterisk/image next to each when validation fails, I need to display custom content (change the outline color of the input textbox, display a tooltip, etc.). I could use a standard validation control in most cases (e.g., RequiredFieldValidator for a TextBox), except for the display when validation fails.

我已经开始了创建CustomValidators,但我需要各种验证(必填字段,定期EX pressions,范围)做了很多次。这似乎是一种浪费重现这些验证的逻辑,只有这样它可以改变响应输出。在 http://msdn.microsoft.com/en-us/library的MS文档/3w0bs977.aspx 说,一个自定义的响应可以客户端和服务器在这种情况下上完成:在客户端和服务器端,您可以创建自定义的响应,如在控制或字体的颜色变化改变为一个标签上的文本。它给服务器侧的例子,但不给为客户端的方法。什么是处理客户端上的自定义响应的最佳方式?

I've started out creating CustomValidators, but I need to do this many times for various validations (required field, regular expressions, ranges). It seems a waste to recreate the logic of these validators only so that it can change the response output. The MS documentation at http://msdn.microsoft.com/en-us/library/3w0bs977.aspx says a custom response can be done on both client and server in this case: "On both the client and server side you can create a custom response, such as a color change in a control or a font change for text on a label." It gives an example for the server side, but does not give a method for the client side. What is the best way to handle the custom response on the client?

推荐答案

这篇文章可以帮助你:

http://msdn.microsoft.com/en-us/library/ aa479045.aspx

特别是本节(寻找客户端验证,然后在那里,特效):

Particularly this section (look for "Client Side Validation" then under there, "Special Effects"):

<asp:Label id=lblZip runat=server 
   Text="Zip Code:"/> 
<asp:TextBox id=txtZip runat=server 
   OnChange="txtZipOnChange();" /></asp:TextBox><br>
<asp:RegularExpressionValidator id=valZip runat=server
   ControlToValidate=txtZip
   ErrorMessage="Invalid Zip Code" 
   ValidationExpression="[0-9]{5}" /><br>

<script language=javascript>
function txtZipOnChange() {
   // Do nothing if client validation is not active
   if (typeof(Page_Validators) == "undefined")  return;
   // Change the color of the label
   lblZip.style.color = valZip.isvalid ? "Black" : "Red";
}
</script>

有仍然是需要做的,这你可以用一些jQuery或类似的整理一些线路可达

There is still some wiring up that needs to be done, which you may be able to tidy up with some jQuery or the like

这篇关于创建使用ASP.NET验证控件自定义响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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