ASP.NET自定义验证客户端和放大器;服务器端验证不点火自定义、放大器、服务器端、客户端

2023-09-02 01:52:33 作者:一纸荒芜╮难描少年眉间伤

这并没有发生在我身上过,但出于某种原因,客户端和服务器端验证事件不会被触发:

 < ASP:文本框ID =TextBoxDTownCity=服务器的CssClass =contactfield/>
< ASP:的CustomValidator ID =CustomValidator2=服务器EnableClientScript =真
    的ErrorMessage =交货城镇或城市需要
    ClientValidationFunction =TextBoxDTownCityClient
    的ControlToValidate =TextBoxDTownCity
    OnServerValidate =TextBoxDTownCity_Validate显示=动态>
< / ASP:的CustomValidator>
 

服务器端验证活动:

 保护无效TextBoxDTownCity_Validate(对象源,ServerValidateEventArgs参数)
{
    args.IsValid = FALSE;
}
 
outlook提示错误 您的服务器不支持此客户端支持的任何验证方式

客户端验证活动:

 函数TextBoxDCountyClient(发件人,参数){
    args.IsValid = FALSE;
    警报(测试);
}
 

我认为在至少在服务器端验证将开火,但没有。这从未发生在我身上了。这真的让我难住了。

我看了看输出和ASP.NET正在识别客户端功能:

ASP.NET JavaScript的输出:

  VAR ctl00_ctl00_content_content_CustomValidator2 =的document.all?的document.all [ctl00_ctl00_content_content_CustomValidator2]:的document.getElementById(ctl00_ctl00_content_content_CustomValidator2);

ctl00_ctl00_content_content_CustomValidator2.controltovalidate =ctl00_ctl00_content_content_TextBoxDTownCity;

ctl00_ctl00_content_content_CustomValidator2.errormessage =交货城镇或城市需要;

ctl00_ctl00_content_content_CustomValidator2.display =动态;

ctl00_ctl00_content_content_CustomValidator2.evaluationfunction =CustomValidatorEvaluateIsValid;

ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction =TextBoxDTownCityClient;
 

渲染自定义的验证:

 <跨度ID =ctl00_ctl00_content_content_CustomValidator2的风格=颜色:红色;显示:无;>交货城镇或城市需要和LT; / SPAN>
 

任何一个可以提供一些线索,为什么客户端和服务器端验证将不会开火。

编辑:错别字我在错误的粘贴功能,问题还是一样

只是另一个更新的最后一个注释:其中的文本框不能为空。我测试了这一点,这是不正确的。在一个空白页没有价值的CustomValidator解雇我的客户端验证功能的罚款:

 < ASP:文本框ID =TextBox1的=服务器/>
< ASP:的CustomValidator ID =CustomValidator1=服务器
的ErrorMessage =的CustomValidatorClientValidationFunction =TextBoxDAddress1Client>< / ASP:的CustomValidator>
< ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/>
 

解决方案

的CustomValidator 将仅当文本框不为空。

如果您需要确保它不是空的,那么你就需要一个RequiredFieldValidator了。

  

Note:如果输入控制是空的,   没有验证函数调用,   验证成功。用一个   RequiredFieldValidator控件   要求用户输入在数据   输入控制。

编辑:

如果你的的CustomValidator 指定的ControlToValidate 属性(和你原来的例子一样),然后你的验证功能将只当控件不为空。

如果您不指定的ControlToValidate 那么你的验证函数都会被调用一次。

此打开了一个第二可能解决问题的方法。而不是使用一个单独的的RequiredFieldValidator ,你可以省略了的ControlToValidate 属性的CustomValidator 和设置您的验证功能,做这样的事情:

客户端code(JavaScript)的:

 函数TextBoxDCountyClient(发件人,参数){
    变种V =的document.getElementById('<%= TextBoxDTownCity.ClientID%>')。值;
    如果(ⅴ==''){
        args.IsValid = FALSE; //字段为空
    }
    其他 {
        //这里做你的其他验证测试...
    }
}
 

服务器端code(C#):

 保护无效TextBoxDTownCity_Validate(
    对象源,ServerValidateEventArgs参数)
{
    串V = TextBoxDTownCity.Text;
    如果(V ==的String.Empty)
    {
        args.IsValid = FALSE; //字段为空
    }
    其他
    {
        //这里做你的其他验证测试...
    }
}
 

This has not happened to me before, but for some reason both the client and server side validation events are not being triggered:

<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
    ErrorMessage="Delivery Town or City required"
    ClientValidationFunction="TextBoxDTownCityClient" 
    ControlToValidate="TextBoxDTownCity"
    OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>

Server-side validation event:

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

Client-side validation event:

function TextBoxDCountyClient(sender, args) {
    args.IsValid = false;
    alert("test");
}

I thought at the least the Server Side validation would fire but no. this has never happened to me before. This has really got me stumped.

I looked at the output and ASP.NET is recognizing the client side function:

ASP.NET JavaScript output:

var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");

ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";

ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";

ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";

ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";

ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";

Rendered custom validator:

<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span> 

Can any one shed some light as to why both client and server side validation would not be firing.

Edit: Typo I pasted in the wrong function, problem still the same

Just another update to the last comment: where by the TextBox cannot be empty. I tested this out and it is not true. On a blank page the CustomValidator fired my client side validation function fine without a value:

<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

解决方案

Your CustomValidator will only fire when the TextBox isn't empty.

If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

EDIT:

If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

If you don't specify ControlToValidate then your validation functions will be called every time.

This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

Client Side code (Javascript):

function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}

Server side code (C#):

protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}