问题描述
我也碰到这个问题之前,但从来没有完全解决它。我有几个验证一个表单,也是一个的CustomValidator。
I have run into this problem before but never quite solved it. I have a form with several validators and also a CustomValidator.
<asp:Label ID="lblMemberNum" runat="server" Text="Membership #:" CssClass="LabelMedium" ></asp:Label>
<asp:TextBox ID="txtMemberNum" runat="server" CssClass="TextBox" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMemberNum" SetFocusOnError="True" runat="server"
ControlToValidate="txtMemberNum" ErrorMessage="[ Membership # ] is required"
CssClass="ValidationMessage" Display="Dynamic" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revMemberNum" Display="Dynamic" runat="server"
ControlToValidate="txtMemberNum" CssClass="ValidationMessage"
ErrorMessage="[ Membership # ] can only contain letters"
ValidationExpression="^([a-zA-Z\d]+)$" >*</asp:RegularExpressionValidator>
<asp:CustomValidator ID="cvMemberNum" runat="server"
CssClass="ValidationMessage" Display="Dynamic"
ControlToValidate="txtMemberNum" ValidateEmptyText="false"
OnServerValidate="cvMemberNum_Validate"
ErrorMessage="This membership number is already registered">*</asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
CssClass="ValidationMessage"
ShowMessageBox="True" ShowSummary="False" />
和服务器端:
protected void cvMemberNum_Validate(object source, ServerValidateEventArgs args)
{
try
{
args.IsValid = (!CampaignRegistration.IsMemberRegistered(args.Value));
}
catch
{
args.IsValid = false;
}
}
我的问题是:在ValidationSummary永远不会从显示的的CustomValidator消息。这个问题已经被问在几个地方,但我还没有看到一个满意的答复。
My problem is: The ValidationSummary never shows the message from CustomValidator. This question has been asked in several places, but I havent seen a satisfactory answer.
推荐答案
尝试使用在所有验证器和在ValidationSummary一个ValidationGroup属性。
Try using a ValidationGroup property across all your validators and the ValidationSummary.
编辑:另一种可能是服务器验证code
Another possibility could be the Server Validation Code
args.IsValid = (!CampaignRegistration.IsMemberRegistered(args.Value));
如果 CampaignRegistration.IsMemberRegistered(args.Value)
终止返回false,!为使其真实,因此使它有效。我想你应该摆脱的!如下:
if CampaignRegistration.IsMemberRegistered(args.Value)
is returning false, "!" is making it true and therefore making it valid. I think you should get rid of the "!" as follows:
args.IsValid = CampaignRegistration.IsMemberRegistered(args.Value);
更新:为了在ValidationSummary在一个消息显示自定义的验证信息,你需要有ClientValidationFunction code。如果你需要没有弹出只显示摘要,这是没有必要的。
UPDATE: In order for the ValidationSummary to display your custom validator message in a messagebox, you need to have ClientValidationFunction Code. If you need to display just the summary without a popup, this is not needed.
<asp:CustomValidator ID="cvMemberNum" runat="server"
CssClass="ValidationMessage" Display="Dynamic"
ControlToValidate="txtMemberNum" ValidateEmptyText="false"
OnServerValidate="cvMemberNum_Validate"
ClientValidationFunction = "ClientValidate"
ErrorMessage="This membership number is already registered">*</asp:CustomValidator>
//JavaScript Code.
function ClientValidate(source, args)
{
args.IsValid = false; //you need to add validation logic here
}
更多:如果您不想做客户方确认,尝试这一招示警戒。进行此更改您的CustomValidator的ServerValidate方式:
MORE: If you don't want to do ClientSide Validation, try this trick to show the alert. Make this change to your CustomValidator ServerValidate method:
protected void cvMemberNum_Validate(object source, ServerValidateEventArgs args)
{
bool isValid = true;
try
{
isValid = (!CampaignRegistration.IsMemberRegistered(args.Value));
}
catch
{
isValid = false;
}
args.IsValid = isValid;
if(!isValid)
{
if(!Page.IsClientScriptBlockRegistered("CustomValidation"))
Page.RegisterClientScriptBlock("CustomValidation", "<script>alert('This membership number is already registered');</script>");
}
}
这篇关于优雅的方式来使用的ValidationSummary消息框的CustomValidator工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!