我试图让CustomValidator调用服务器方法(它是一个校验和,我认为它是正确的,但是你会发现我无法告诉它)但它从未被调用,我可以在文本框中输入任何内容并且错误消息永远不会出现,并且在方法中添加断点表明它根本不会被调用,无论什么&#39 ; s在文本框中输入。我是否也需要客户端验证?
我的OnServerValidate看起来正确地调用了我的眼睛方法 - 我真的很茫然。非常感谢!
ASP.Net代码:
<asp:Label ID="lblPPSN" runat="server" Text="PPS number: "></asp:Label>
<asp:TextBox ID="tbxPPSN" runat="server"></asp:TextBox>
<asp:CustomValidator ID="customval_PPSN" runat="server" ErrorMessage="Please enter a valid PPSN" ControlToValidate="tbxPPSN"
OnServerValidate="customval_PPSN_ServerValidate" ForeColor="Red"></asp:CustomValidator>
c#代码隐藏:
protected void customval_PPSN_ServerValidate(object source, ServerValidateEventArgs args)
{
string checkChar = "", formatChar = "", input = tbxPPSN.Text;
bool newFormat = false;
if (input.Length > 9)
{
args.IsValid = false;
return;
}
if (input.Length == 9)
{
newFormat = true;
}
char[] PPSArray = input.ToCharArray();
Array.Reverse(PPSArray);
checkChar = PPSArray[0].ToString();
if (newFormat)
{
checkChar = PPSArray[1].ToString();
formatChar = PPSArray[0].ToString();
}
checkChar = checkChar.ToLower();
formatChar = formatChar.ToLower();
int seed = 2, PPSParsed = 0, PPSsum = 0, PPSMod = 0;
foreach (char ppschar in PPSArray)
{
if (int.TryParse(ppschar.ToString(), out PPSParsed))
{
PPSsum += PPSParsed * seed;
seed++;
}
}
if (newFormat)
{
PPSsum += Convert.ToInt32(formatChar.ToCharArray()[0] - 96) * 9;
}
PPSMod = PPSsum % 23;
if (PPSMod == 0) PPSMod = 23;
if (Convert.ToString((char) (96 + PPSMod)).ToLower() == checkChar)
{
args.IsValid = true;
return;
}
else
{
args.IsValid = false;
}
}
答案 0 :(得分:1)
如果您从未回发到服务器,则不会调用服务器端验证。您可能希望进行一些客户端验证,以便您的用户无需提交表单以获取其验证结果。
这篇MSDN文章有一个使用服务器端和客户端验证的例子: CustomValidator Example
我使用了你的标记和代码隐藏,它似乎有效。
标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Val.aspx.cs" Inherits="CustomValidatorExample.Val" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblPPSN" runat="server" Text="PPS number: "></asp:Label>
<asp:TextBox ID="tbxPPSN" runat="server"></asp:TextBox>
<asp:CustomValidator ID="customval_PPSN" runat="server" ErrorMessage="Please enter a valid PPSN" ControlToValidate="tbxPPSN"
OnServerValidate="customval_PPSN_ServerValidate" ForeColor="Red"></asp:CustomValidator>
<asp:Button runat="server" ID="button1" Text="Validate" OnClick="button1_Click" />
</div>
</form>
</body>
</html>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomValidatorExample
{
public partial class Val : System.Web.UI.Page
{
protected void customval_PPSN_ServerValidate(object source, ServerValidateEventArgs args)
{
string checkChar = "", formatChar = "", input = tbxPPSN.Text;
bool newFormat = false;
if (input.Length > 9)
{
args.IsValid = false;
return;
}
if (input.Length == 9)
{
newFormat = true;
}
char[] PPSArray = input.ToCharArray();
Array.Reverse(PPSArray);
checkChar = PPSArray[0].ToString();
if (newFormat)
{
checkChar = PPSArray[1].ToString();
formatChar = PPSArray[0].ToString();
}
checkChar = checkChar.ToLower();
formatChar = formatChar.ToLower();
int seed = 2, PPSParsed = 0, PPSsum = 0, PPSMod = 0;
foreach (char ppschar in PPSArray)
{
if (int.TryParse(ppschar.ToString(), out PPSParsed))
{
PPSsum += PPSParsed * seed;
seed++;
}
}
if (newFormat)
{
PPSsum += Convert.ToInt32(formatChar.ToCharArray()[0] - 96) * 9;
}
PPSMod = PPSsum % 23;
if (PPSMod == 0) PPSMod = 23;
if (Convert.ToString((char)(96 + PPSMod)).ToLower() == checkChar)
{
args.IsValid = true;
return;
}
else
{
args.IsValid = false;
}
}
protected void button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write("Valid");
}
}
}
}