有没有一种方法来获得的ValidationSummary与客户端的验证工作? [英] Is there a way to get a ValidationSummary to work with client-side validators?

查看:97
本文介绍了有没有一种方法来获得的ValidationSummary与客户端的验证工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从被解雇的回发验证的.NET ValidationSummary控件聚集的错误消息。有没有得到它也从客户端的验证器会显示错误消息的某种方式?

The .NET ValidationSummary control aggregates error messages from validators that are fired on postback. Is there some way of getting it to also show error messages from client-side validators?

我的工作的问题是,在一个很长的网页,就可以点击提交按钮,有一些客户端验证失败,但从来没有看到错误消息。我想有近按钮验证摘要,使用户永远不会离开没有反馈。

The problem I am working on is that on a long page, it is possible to click a submit button, have some client-side validators fail, but never see the error message. I would like to have a validation summary near the button so that the user is never left without feedback.

我想有客户端验证,以反映在验证摘要任何标准的.NET验证,但我最关心的是获得RequiredFieldValidators工作。

I would like any standard .NET validators that have client-side validation to be reflected in the validation summary, but I am most concerned with getting the RequiredFieldValidators to work.

我可以砍它,以便在单击该按钮会自动,在短暂的延迟后,显示一般的信息,告诉用户来检查网页上有错误。这样的工作没有发生回发时的,但它是丑陋的。

I could hack it so that on clicking the button would automatically, after a short delay, display a general message telling the user to check for errors on the page. This would work for when no postback has occurred, but it would be ugly.

下面是一些例子code不工作对我来说:

Here is some example code that is not working for me:

<asp:LinkButton ID="btnOpen" runat="server" ToolTip="Open" ValidationGroup="Create" CausesValidation="true" />
<asp:TextBox ID="txtBlah" runat="server" />
<asp:RequiredFieldValidator ID="reqBlah" runat="server" ControlToValidate="txtBlah" EnableClientScript="true" Display="Dynamic" ErrorMessage="Enter some blah" ValidationGroup="Create" />
<asp:ValidationSummary ID="summary" runat="server" EnableClientScript="true" DisplayMode="BulletList" HeaderText="Errors:" ShowMessageBox="true" ShowSummary="true" ValidationGroup="Create" />

验证器显示无回传的错误,但总结没有。

The validator shows the error without a postback, but the summary does not.

由于已经提出的意见,有可能是从工作停止我的例子code页面上的其他东西。我不能够尝试重现的确切原因现在,虽然网页是pretty的沉重,对有一定的UpdatePanel,所以我就坚持我砍,直到有更多的时间来进行这项工作。

As has been suggested in the comments, there is probably something else on the page that stops my example code from working. I'm not able to try to reproduce the exact cause right now, though the page is pretty heavy and has some UpdatePanels on there, so I'll just stick with my hack until there is more time to work on it.

感谢试图帮助。我建议人们不要在这个问题上的任何工作多了,因为我不认为我已经提供了足够的信息来真正帮助人解决问题。

Thanks for the attempts to help. I recommend people not to work on this question any more, as I don't think I've provided enough info to really help anyone solve the issue.

推荐答案

我也有这个问题。花费了太多的时间解剖MS客户端验证API之后),我跟踪这个问题到一个奇怪的问题,DOM解析,下面我解释为什么发生这种情况在一些案件和解决方案,我来修复它。

I too have had this problem. After spending way too much time dissecting the MS client side validation API ;), I traced the problem to a weird issue with DOM parsing, below I explain why this happens in some cases and the solution I used to fix it.

[为什么会发生?]

每个验证器控制呈现为与用于执行客户端验证非标准属性的跨度。例如下面的evaluationfunction属性:

Each validator control is rendered as a span with non-standard attributes that are used to perform the client side validation. For example the evaluationfunction attribute below:

<span id="...id" controltovalidate="...BillingName" errormessage="blah" evaluationfunction="RequiredFieldValidatorEvaluateIsValid" display="Dynamic" initialvalue="" >*</span>

这些属性由这样下面的API中的框架解析(注意的eval):

These attributes are parsed by the framework in the API like this below (notice the eval):

for (i = 0; i < Page_Validators.length; i++) {
  val = Page_Validators[i];
  if (typeof(val.evaluationfunction) == "string") {
     eval("val.evaluationfunction = " + val.evaluationfunction + ";");
     } 
}

的问题是,特殊的属性,即evaluationfunction我们总是返回undefined因此字符串从来没有转化为正确的验证对象。这对我来说仍然是个谜,因为从我可以告诉这似乎是完全随机的。

the problem is that the special attributes i.e. evaluationfunction we're always returning undefined so the string was never converted to a proper validator object. This to me is still a mystery because from what I can tell it appears to be totally random.

在Page_ClientValidate是拉开序幕,它试图调用每个验证器验证的功能,但它不能因为evaluationfunction ==不确定会发生什么事。除了使用假作为默认的它假定真实,所以没有确认实际发生的,一切都似乎从客户端有效。在如果(typeof运算(val.evaluationfunction)==功能)是不正确的,因此倒在事先指定的 val.isvalid = TRUE;

What happens is when the Page_ClientValidate is kicked off, it tries to invoke each validators validate function but it can't because evaluationfunction == undefined. Instead of using false as the default it assumes true and so no validation actually happens and everything appears valid from the client side. the if (typeof(val.evaluationfunction) == "function") is never true so it falls back on the prior assignment of val.isvalid = true;.

function ValidatorValidate(val, validationGroup, event) {
 val.isvalid = true;
 if ((typeof(val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) 
 {
  if (typeof(val.evaluationfunction) == "function") 
  {
   val.isvalid = val.evaluationfunction(val);
   if (!val.isvalid && Page_InvalidControlToBeFocused == null && typeof(val.focusOnError) == "string" && val.focusOnError == "t") 
   {
    ValidatorSetFocus(val, event);
   }
  }
 }
  ValidatorUpdateDisplay(val);
} 

[我怎么解决这个问题?]

要解决这个问题我写了一个,可以在DOM加载完成后调用程序。该程序循环的所有验证控件,并试图在使用JQuery SPAN的创建从原始标记数据对象的属性,虽然你很可能使用任何其他等效工具来得到同样的结果。这个程序不能解决所有的验证控件,主要是需要现场校验和定期EX pression验证。你需要的,如果你使用具有附加属性的其他验证控件来改变它。

To fix this I wrote a routine that can be called after the DOM has finished loading. This routine loops all of the validator controls and tries to create the object properties from the raw markup data in the SPAN using JQuery, although you could probably use any other equivalent tool to get the same result. This routine does not fix all of the validator controls, mainly required field validators and regular expression validators. You'll need to change it if your using other validation controls that have additional properties.

function fixValidatorProperties()
{
    if (Page_Validators && Page_Validators[0] && Page_Validators[0].evaluationfunction == undefined)
    {
        var val = null;
        for (i = 0; i < Page_Validators.length; i++) 
        {
            val = Page_Validators[i];

            if (val.initialvalue == undefined)
                val.initialvalue = "";

            if ($(val).attr("evaluationfunction"))
                  eval("val.evaluationfunction = " + $(val).attr("evaluationfunction") + ";");
            if ($(val).attr("controltovalidate"))
                  val.controltovalidate = $(val).attr("controltovalidate");
            if ($(val).attr("errormessage"))
                  val.errormessage = $(val).attr("errormessage");
            if ($(val).attr("Dynamic"))
                  val.Dynamic = $(val).attr("Dynamic");     
            if ($(val).attr("initialvalue"))
                  val.initialvalue = $(val).attr("initialvalue"); 
                if ($(val).attr("ValidationExpression"))  
                  val.validationexpression =  $(val).attr("ValidationExpression");
         }     
    }
}

这篇关于有没有一种方法来获得的ValidationSummary与客户端的验证工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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