asp.net自定义验证器不点火的文本框 [英] asp.net custom validator not firing for textbox

查看:140
本文介绍了asp.net自定义验证器不点火的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都用于验证texbox必填字段校验器和自定义验证。所需的字段校验火灾完美。 ?我无法得到自定义验证正确火

 < ASP:文本框ID =txtPRIRUNAT = 服务器WIDTH =295/>< BR /> 

< ASP:的RequiredFieldValidator显示=动态的CssClass =leftAlignSetFocusOnError =真=服务器的ControlToValidate =txtPRI的ErrorMessage =请输入您的PRI/>

< ASP:的CustomValidator =服务器ID =cusCustom的ControlToValidate =txtPRIonservervalidate =cusCustom_ServerValidate启用=真ValidateEmptyText =真显示=动态的CssClass = leftAlignSetFocusOnError =真的ErrorMessage =文字必须正好8个字符! />



背后

代码

 保护无效cusCustom_ServerValidate(对象发件人,ServerValidateEventArgs E)
{
的Response.Write(射击 - 测试);
到Response.End();


如果(e.Value.Length == 8)
e.IsValid = TRUE;
,否则
e.IsValid = FALSE;
}


解决方案

检查,你有你的的CustomValidator 属性 ValidateEmptyText 设置为真正使空文本会进行验证。然后你将不再需要在的RequiredFieldValidator 了。



编辑:我把你的代码复制并粘贴它到一个空的项目和它的作品如预期。一定有什么东西你还没有发布,或发布不正确的,我们是不知道的。是否还有其他影响正在触发验证或验证控件本身的按钮



编辑:这是确切的代码(这是在内容页):



aspx页面:

 < ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent> 
< ASP:文本框ID =txtPRI=服务器WIDTH =295/>< BR />
< ASP:的RequiredFieldValidator ID =RequiredFieldValidator1显示=动态的CssClass =leftAlignSetFocusOnError =真=服务器的ControlToValidate =txtPRI的ErrorMessage =请输入您的PRI/>
< ASP:的CustomValidator =服务器ID =cusCustom的ControlToValidate =txtPRIonservervalidate =cusCustom_ServerValidate启用=真ValidateEmptyText =真显示=动态的CssClass =leftAlignSetFocusOnError =真的ErrorMessage =文字必须正好8个字符! />
< / ASP:内容>



的.cs页(空的Page_Load ):

 保护无效cusCustom_ServerValidate(对象发件人,ServerValidateEventArgs E)
{
//这里放一个破发点它停在它
如果(e.Value.Length == 8)
e.IsValid = TRUE;
,否则
e.IsValid = FALSE;
}


I have both a required field validator and custom validator for validating a texbox. The required field validator fires perfectly. I'm not able to get the custom validator to fire properly?

<asp:TextBox ID="txtPRI" runat="server" Width="295" /><br />

<asp:RequiredFieldValidator display="Dynamic" CssClass="leftAlign" SetFocusOnError="true"  runat="server" controltovalidate="txtPRI" errormessage="Please enter your PRI" />

 <asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtPRI" onservervalidate="cusCustom_ServerValidate" Enabled="true" ValidateEmptyText="true" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true"  errormessage="The text must be exactly 8 characters long!" />

code behind

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        Response.Write("firing - test");
        Response.End();


        if (e.Value.Length == 8)
            e.IsValid = true;
        else
            e.IsValid = false;
    }

解决方案

Check that you have the your CustomValidator property ValidateEmptyText set to true so that empty text will be validated. Then you will not need the RequiredFieldValidator anymore.

EDIT: I took your code and copy and pasted it into an empty project and it works as expected. There must be something you have not posted, or is posting incorrectly, that we are not aware of. Is there anything else that affects the button that is triggering the validation or the validation controls themselves?

EDIT: Here is the exact code (it's in a content page):

aspx page:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:TextBox ID="txtPRI" runat="server" Width="295" /><br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true"  runat="server" controltovalidate="txtPRI" errormessage="Please enter your PRI" />  
    <asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtPRI" onservervalidate="cusCustom_ServerValidate" Enabled="true" ValidateEmptyText="true" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true"  errormessage="The text must be exactly 8 characters long!" /> 
</asp:Content>

.cs page (empty Page_Load):

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{ 
    // put a break point here and it stops on it
    if (e.Value.Length == 8)
        e.IsValid = true;
    else
        e.IsValid = false;
} 

这篇关于asp.net自定义验证器不点火的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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