开发者

Why The error message for custom validator is not shown in message box?

I have tried in many way but the error message for custom validator is not shown in validation summary but it(ValidationSummary) shows error message for every other type of validator.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Expt_Custom Validator.aspx.cs" Inherits="Expt_Custom_Validator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script runat="server">
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Equals("J开发者_Go百科agdeep"))
                args.IsValid = false;
            else
                args.IsValid = true;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblName" runat="server" Text="Enter Your Name"></asp:Label>
        <asp:TextBox ID="txtbxName" runat="server"></asp:TextBox>

        <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="You are Not allowed" Display="None" 
            onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
        <br />
        <asp:Label ID="lblClass" runat="server" Text="Class"></asp:Label>
        <asp:TextBox ID="txtClass" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="Please enter Clas" ControlToValidate="txtClass" Display="None"></asp:RequiredFieldValidator>
        <br />

        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Validate" />

    </div>
    </form>
</body>
</html>


Custom Validator, when placing in formview will not show its error message after server-side validation (though it has been validated and result is invalid) the mean to fix this in to wrap it by a Update Panel.


I solved this issue by registering a client script block, which updates the validation summary content. I have used jQuery for this:

 private void DisplayCustomValidationMessage(CustomValidator cv, ValidationSummary vs)
 {
      if ((cv == null) || (vs == null)) return;

      Type csType = this.GetType();

      if (ClientScript.IsClientScriptBlockRegistered(csType, cv.ID)) return;

      StringBuilder sb = new StringBuilder(@"<script type='text/javascript'>");
      sb.Append(@"$(function () {");
      sb.Append(@"var $vs = $('#" + vs.ClientID + "');");
      sb.Append(@"if($vs.find('ul').length){");
      sb.Append(@"$vs.find('ul').append('<li>" + cv.ErrorMessage + "</li>');");
      sb.Append(@"}else{");
      sb.Append(@"$vs.html('Fehlerhafte Eingabe(n):<ul><li>" + cv.ErrorMessage + "</li></ul>'); }");
      sb.Append(@"$vs.css('display', 'block');");
      sb.Append(@"});");
      sb.Append(@"</script>");

      ClientScript.RegisterClientScriptBlock(csType, cv.ID, sb.ToString());
 }

The method has to be called if validation fails:

protected void cvYourCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = YourCustomValidationMethod();

        if (!args.IsValid)
        {
          DisplayCustomValidationMessage((CustomValidator) source, vsYourValidationSummaryControl);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜