gpt4 book ai didi

c# - 自定义验证器触发但不更新 ValidationSummary

转载 作者:行者123 更新时间:2023-11-30 14:41:18 26 4
gpt4 key购买 nike

您好,我正在使用自定义表单字段验证器,似乎自定义验证器正在工作,不允许它继续到下一页,但它不会更新验证摘要,也不会显示星号和我使可见的标签。我在同一字段上还有其他验证器,如 RequiredFieldValidator。我的 ValidationGroup 已设置,Text 和 IsValid 也已设置。我什至按照一些解决方法的建议,在 javascript 中编写并设置了一个虚拟的客户端验证方法。

这里是asp.net中的验证摘要代码

<asp:ValidationSummary ID="ValidatorSummary" runat="server" ValidationGroup="Step2" />

这里是自定义验证器和必填字段一

<asp:CustomValidator ID="AddressVerification" runat="server" ErrorMessage="Please enter a valid address." Display="Dynamic" ValidationGroup="Step2" OnServerValidate="AddressVerification_ServerValidate" ClientValidationFunction="CustomValidatorDummy" Text="*" Enabled="true" EnableClientScript="true"></asp:CustomValidator>
<asp:RequiredFieldValidator ID="RFValidatorHomeAddress" runat="server" ErrorMessage="Please enter home address." Text="*" Display="Dynamic" ValidationGroup="Step2" ControlToValidate="txtHomeAddress"></asp:RequiredFieldValidator>

这里是后面代码中的自定义验证方法

protected void AddressVerification_ServerValidate(object sender, ServerValidateEventArgs e)
{
//lets just say it doesn't validate and sets the IsValid to false
lblUspsValidatorResHomeCity.Visible = true;
lblUspsValidatorResHomeState.Visible = true;
lblUspsValidatorResHomeZip.Visible = true;
e.IsValid = false;
}

请指教,谢谢。

编辑:已回答 - 如 bitxwise 所述。验证摘要也应放在更新面板中。谢谢!

像这样:

<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="false" UpdateMode="Conditional"
runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="AddressHomeValidationSummary" runat="server" ValidationGroup="AddressHomeValidationGroup"
CssClass="errors" />
</ContentTemplate>

然后调用更新:

UpdatePanelValidationSummaryHome.Update();

最佳答案

您的 CustomValidator 声明中似乎缺少 ControlToValidate

编辑

如果您的 CustomValidator 聚合了多个控件,那么试试这个:

ASPX

<asp:TextBox ID="txtMyTextBox" runat="server" />
<asp:CustomValidator ID="AddressVerification" runat="server"
Display="Dynamic"
ErrorMessage="Please enter a valid address."
OnServerValidate="AddressVerification_ServerValidate"
Text="*"
ValidationGroup="Step2" />
<asp:RequiredFieldValidator ID="rfvAddress" runat="server"
ControlToValidate="txtMyTextBox"
Display="Dynamic"
ErrorMessage="Please enter an address"
Text="*"
ValidationGroup="Step2" />
...
<asp:ValidationSummary ID="ValidatorSummary" runat="server"
ValidationGroup="Step2" />
...
<asp:Button ID="btnCheckAddresses" runat="server"
CausesValidation="true"
Text="Check Addresses"
ValidationGroup="Step2" />

CS

protected void AddressVerification_ServerValidate(object source, ServerValidateEventArgs args) {
args.IsValid = !string.IsNullOrEmpty(txtMyTextBox.Text) && !txtMyTextBox.Text.Contains(' ');
}

请注意,调用回发的控件的验证组具有 CausesValidation="true" 并且具有与验证器相同的 ValidationGroup

编辑 2

如果您的回发控件位于 UpdatePanelValidationSummary 不在,则部分回发不会刷新 ValidationSummary。一旦您从 UpdatePanel 中删除回发控件,我想它会生成一个完整的回发,这将刷新您的 ValidationSummary

我不知道您的UpdatePanel 中还有什么,但是many people report having issues with their validators being in UpdatePanel's .

查看 MSDN ,

When you use the ValidationSummary control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

还有这个MSDN blog .

关于c# - 自定义验证器触发但不更新 ValidationSummary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com