定制验证射击,但它不会更新在ValidationSummary [英] Custom Validator firing but it does not update the ValidationSummary

查看:230
本文介绍了定制验证射击,但它不会更新在ValidationSummary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个自定义表单字段验证工作,好像自定义验证是通过不允许其继续到下一个页面的工作,但它不会更新验证摘要也不会显示星号和我做了visable标签。我也有在同一领域的其他类似验证程序的RequiredFieldValidator。我的ValidationGroup设置,因为是文本和的IsValid。我甚至写了并且在JavaScript中设置一个虚拟的客户端验证方法,一些解决方法建议。

下面是验证摘要code在asp.net

 < ASP:的ValidationSummary ID =ValidatorSummary=服务器的ValidationGroup =第二步/>

这里是自定义验证和所需的字段中的一个

 < ASP:的CustomValidator ID =AddressVerification=服务器的ErrorMessage =请输入一个有效的地址。显示=动态的ValidationGroup =第二步OnServerValidate =AddressVerification_ServerValidateClientValidationFunction =CustomValidatorDummy文本=*启用=真EnableClientScript =真>< / ASP:的CustomValidator>
< ASP:的RequiredFieldValidator ID =RFValidatorHomeAddress=服务器的ErrorMessage =请输入家庭住址。文本=*显示=动态的ValidationGroup =第二步的ControlToValidate =txtHomeAddress>< / ASP:&的RequiredFieldValidator GT;

下面是在code中的自定义验证方法背后

 保护无效AddressVerification_ServerValidate(对象发件人,ServerValidateEventArgs E)
{
//让只说这不验证,并设置的IsValid为false
lblUspsValidatorResHomeCity.Visible = TRUE;
lblUspsValidatorResHomeState.Visible = TRUE;
lblUspsValidatorResHomeZip.Visible = TRUE;
e.IsValid = FALSE;
}

请指教,谢谢。

编辑:
回答 - 因为bitxwise提及。验证摘要应放在更新面板内为好。谢谢!

像这样:

 < ASP:的UpdatePanel ID =UpdatePanelValidationSummaryHomeChildrenAsTriggers =假的UpdateMode =条件
=服务器>
<&的ContentTemplate GT;
    < ASP:的ValidationSummary ID =AddressHomeValidationSummary=服务器的ValidationGroup =AddressHomeValidationGroup
        的CssClass =错误/>
< /&的ContentTemplate GT;

,然后调用更新:

  UpdatePanelValidationSummaryHome.Update();


解决方案

您似乎缺少的ControlToValidate 的CustomValidator

修改

如果您的CustomValidator汇聚了多个控件,那么试试这个:

ASPX

 < ASP:文本框ID =txtMyTextBox=服务器/>
< ASP:的CustomValidator ID =AddressVerification=服务器
    显示=动态
    的ErrorMessage =请输入一个有效的地址。
    OnServerValidate =AddressVerification_ServerValidate
    文本=*
    的ValidationGroup =第二步/>
< ASP:的RequiredFieldValidator ID =rfvAddress=服务器
    的ControlToValidate =txtMyTextBox
    显示=动态
    的ErrorMessage =请输入地址
    文本=*
    的ValidationGroup =第二步/>
...
< ASP:的ValidationSummary ID =ValidatorSummary=服务器
    的ValidationGroup =第二步/>
...
< ASP:按钮的ID =btnCheckAddresses=服务器
    的CausesValidation =真
    文本=检查地址
    的ValidationGroup =第二步/>

CS

 保护无效AddressVerification_ServerValidate(对象源,ServerValidateEventArgs参数){
    !args.IsValid = string.IsNullOrEmpty(txtMyTextBox.Text)及&放大器; !txtMyTextBox.Text.Contains('');
}

请注意,控制的调用回发验证组有的CausesValidation =真正的,并且具有相同的ValidationGroup 的验证。

编辑2

如果您的回传控制是在的UpdatePanel 的ValidationSummary 不是,那么部分回发不会有刷新的ValidationSummary 。一旦你删除从的UpdatePanel 回传控制,我想那会产生一个完整的回发,这将刷新你的的ValidationSummary

我不知道在你的的UpdatePanel ,而<一还有什么href=\"http://www.eggheadcafe.com/community/aspnet/7/10060098/validation-is-not-working-in-update-panel.aspx\">many人用自己的验证在的UpdatePanel 的被报告有问题。

查看<一个href=\"http://msdn.microsoft.com/en-us/system.web.ui.webcontrols.validationsummary.aspx\">MSDN,


  

当您使用的ValidationSummary
  UpdatePanel控件内部控制,
  请确保验证控件
  并且控制与它相关联
  是在相同的面板上。欲了解更多
  有关使用信息
  UpdatePanel控件的部分页面
  更新,请参阅部分页呈现
  概述


以及本 MSDN博客

Hi I am working on a custom form field validator, it seems like the custom validator is working by not allowing it to continue to the next page, but it doesn't update the Validation Summary nor does it display the asterisk and the labels that i've made visable. I also have other validators like RequiredFieldValidator on the same field. My ValidationGroup is set, as is the Text and IsValid. I even wrote and set a dummy client side validation method in javascript as some workarounds suggests.

here is the validation summary code in asp.net

<asp:ValidationSummary ID="ValidatorSummary" runat="server" ValidationGroup="Step2" />

here is the custom validator and the required field one

<asp:CustomValidator ID="AddressVerification" runat="server" ErrorMessage="Please enter a valid address." Display="Dynamic" ValidationGroup="Step2" OnServerValidate="AddressVerification_ServerValidate" ClientValidationFunction="CustomValidatorDummy" Text="*" Enabled="true" EnableClientScript="true"></asp:CustomValidator>
<asp:RequiredFieldValidator ID="RFValidatorHomeAddress" runat="server" ErrorMessage="Please enter home address." Text="*" Display="Dynamic" ValidationGroup="Step2" ControlToValidate="txtHomeAddress"></asp:RequiredFieldValidator>

here is the custom validation method in the code behind

protected void AddressVerification_ServerValidate(object sender, ServerValidateEventArgs e)
{
//lets just say it doesn't validate and sets the IsValid to false
lblUspsValidatorResHomeCity.Visible = true;
lblUspsValidatorResHomeState.Visible = true;
lblUspsValidatorResHomeZip.Visible = true;
e.IsValid = false;
}

please advise, thanks.

EDIT: Answered - as bitxwise mentioned. the validation summary should be placed inside an update panel as well. Thanks!

Like so:

<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="false" UpdateMode="Conditional"
runat="server">
<ContentTemplate>
    <asp:ValidationSummary ID="AddressHomeValidationSummary" runat="server" ValidationGroup="AddressHomeValidationGroup"
        CssClass="errors" /> 
</ContentTemplate>

and then calling the update:

UpdatePanelValidationSummaryHome.Update();

解决方案

You seem to be missing ControlToValidate in your declaration of CustomValidator.

EDIT

If your CustomValidator aggregates multiple controls, then try this:

ASPX

<asp:TextBox ID="txtMyTextBox" runat="server" />
<asp:CustomValidator ID="AddressVerification" runat="server"
    Display="Dynamic"
    ErrorMessage="Please enter a valid address."
    OnServerValidate="AddressVerification_ServerValidate"
    Text="*"
    ValidationGroup="Step2" />
<asp:RequiredFieldValidator ID="rfvAddress" runat="server"
    ControlToValidate="txtMyTextBox"
    Display="Dynamic"
    ErrorMessage="Please enter an address"
    Text="*"
    ValidationGroup="Step2" />
...
<asp:ValidationSummary ID="ValidatorSummary" runat="server"
    ValidationGroup="Step2" />
...
<asp:Button ID="btnCheckAddresses" runat="server"
    CausesValidation="true"
    Text="Check Addresses"
    ValidationGroup="Step2" />

CS

protected void AddressVerification_ServerValidate(object source, ServerValidateEventArgs args) {
    args.IsValid = !string.IsNullOrEmpty(txtMyTextBox.Text) && !txtMyTextBox.Text.Contains(' ');
}

Note that the validation group of the control invoking the post back has CausesValidation="true" and has the same ValidationGroup as the validators.

EDIT 2

If your postback control was in the UpdatePanel but the ValidationSummary was not, then the partial postback would not have refreshed the ValidationSummary. Once you removed the postback control from the UpdatePanel, I imagine it would then generate a full postback, which would refresh your ValidationSummary.

I don't know what else is in your UpdatePanel, but many people report having issues with their validators being in UpdatePanel's.

Check out MSDN,

When you use the ValidationSummary control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

as well as this MSDN blog.

这篇关于定制验证射击,但它不会更新在ValidationSummary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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