的CustomValidator的ServerValidate方法不火 [英] CustomValidator ServerValidate method does not fire

查看:90
本文介绍了的CustomValidator的ServerValidate方法不火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把的CustomValidator 我的形式。我还没有设置其的ControlToValidate 属性。在其的ServerValidate 事件我已经写了以下内容:

 保护无效CustomValidator1_ServerValidate(对象源,
                                               ServerValidateEventArgs参数)
{
    args.IsValid = FALSE;
}

我把一个断点这个方法,但它似乎从来没有走到那一步。但是,如果我这样做是另一种形式的它就像一个魅力。


  1. 这两个按钮和的CustomValidator 是相同
  2. 的ValidationGroup 属性
  3. 我想在这两个按钮和的CustomValidator 删除这个属性,仍然不能正常工作。

这似乎有什么东西formwide。我只是把一个的CustomValidator 在表格上,不碰任何不仅仅是设置其的ServerValidate 事件方法等它的属性

编辑:这里的ASPX部分:

 < ASP:的CustomValidator ID =CustomValidator2=服务器
       的ErrorMessage =这是一个测试
   onservervalidate =CustomValidator1_ServerValidate
   的ValidationGroup =PA>< / ASP:的CustomValidator>
< ASP:按钮的ID =btnPensionersOK=服务器文本=OKWIDTH =960x75像素
          的onclick =的button1_Click的ValidationGroup =PA/>


解决方案

尝试通过的 Page.Validate

 保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    Page.Validate();
    如果(Page.IsValid)
    {
       //的ServerValidate应该被称为
    }
}

修改(从评论):

如果您想要的CustomValidator来验证,如果没有被输入/在你的控制选择,你需要设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx\"><$c$c>ValidateEmptyText为true。您还可能希望让的CustomValidator 替换 RequiredFieldValidators

我认为在ASPX的验证顺序决定一个previous验证已经取得了的CustomValidator的severvalidate是否叫 Page.IsValid = FALSE 。或ASP.NET是如此聪明,它假定 SeverValidate 比一个简单的昂贵的文本是空的检查。

I've put a CustomValidator on my form. I have not set its ControlToValidate property. In its ServerValidate event I've written the following:

protected void CustomValidator1_ServerValidate(object source,      
                                               ServerValidateEventArgs args)
{
    args.IsValid = false;
}

I put a breakpoint to this method but it seems to never come to that point. But if I do this on another form it works like a charm.

  1. The ValidationGroup property of both the button and the CustomValidator are the same
  2. I tried deleting this property in both the button and the CustomValidator, still does not work.

It seems as there's something formwide. I just put a CustomValidator on the form and do not touch any of its properties other than just setting its ServerValidate event method.

EDIT: Here's the aspx part:

 <asp:CustomValidator ID="CustomValidator2" runat="server" 
       ErrorMessage="This is a test" 
   onservervalidate="CustomValidator1_ServerValidate" 
   ValidationGroup="PA"></asp:CustomValidator>


<asp:Button ID="btnPensionersOK" runat="server" Text="OK" Width="75px" 
          onclick="Button1_Click" ValidationGroup="PA" />

解决方案

Try to force the validation in the button-click handler via Page.Validate:

protected void Button1_Click(Object sender, EventArgs e)
{
    Page.Validate();
    if(Page.IsValid)
    {
       // servervalidate should have been called
    }
}

Edit(from comments):

If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set ValidateEmptyText to true. You also might want to let the CustomValidator replace the RequiredFieldValidators.

I assume that the validator-order on the aspx decides whether or not a customvalidator's severvalidate is called if a previous Validator already has made Page.IsValid=false. Or ASP.NET is so smart that it assumes the SeverValidate to be costlier than a simple text-is-empty check.

这篇关于的CustomValidator的ServerValidate方法不火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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