ASP.NET 更新面板 - 即使 CausesValidation = false 也会为 AutoPostBack DropDownList 触发必填字段验证器

ASP.NET Update Panel - Required Field Validator fires for AutoPostBack DropDownList even though CausesValidation = false

我遇到一个问题,当它附加到的下拉列表在更改时执行部分回发时,必填字段验证器在屏幕上闪烁。

本质上,该页面是一个表单,其中用户 select 来自第一个下拉列表并填充第二个下拉列表。然后他们从第二个下拉列表中 select。然后,当用户 select 编辑了两者时,他们点击了提交按钮并验证了两个下拉菜单。

两个下拉菜单都在提交按钮使用的验证组中。我已将 CausesValidation="false" 添加到下拉列表,但验证器仍然闪烁。下面的示例代码...

<form id="form1" runat="server">

    <asp:ScriptManager ID="MyAppScriptManager" runat="server"
        EnablePartialRendering="true"
        EnableCdn="true" />

        <div class="chat">

                        <asp:UpdatePanel ID="NewBookingUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
                            <ContentTemplate>

                                    <div id="FreelancerDiv" class="collapse show">
                                        <div class="card-body">
                                            <div class="form-group">
                                                <div class="input-group">
                                                    <asp:DropDownList ID="FreelancerDropDownList" runat="server" AutoPostBack="true"
                                                        CausesValidation="false"
                                                        class="custom-select form-control form-control-lg">
                                                        <asp:ListItem Value=" " Text="Choose the freelancer for your booking." />
                                                        <asp:ListItem Value="Good" Text="Good selection." />
                                                    </asp:DropDownList>
                                                </div>
                                                <asp:RequiredFieldValidator ID="FreelancerRequired" runat="server" Display="Dynamic"
                                                    ControlToValidate="FreelancerDropDownList"
                                                    ValidationGroup="RequestBookingValidationGroup"
                                                    ErrorMessage="&nbsp;Required&nbsp;"
                                                    CssClass="alert-danger" />
                                            </div>
                                        </div>
                                    </div>

                                 
                                    <div id="ServiceDiv" class="collapse show" runat="server">
                                        <div class="card-body">
                                            <div class="form-group">
                                                <div class="input-group">
                                                    <asp:DropDownList ID="ServiceDropDownList" runat="server" AutoPostBack="true"
                                                        CausesValidation="false"
                                                        class="custom-select form-control form-control-lg">
                                                        <asp:ListItem Value=" " Text="Choose the service for your booking." />
                                                        <asp:ListItem Value="Good" Text="Good selection." />
                                                    </asp:DropDownList>
                                                </div>
                                                <asp:RequiredFieldValidator ID="ServiceRequired" runat="server" Display="Dynamic"
                                                    ControlToValidate="ServiceDropDownList"
                                                    ValidationGroup="RequestBookingValidationGroup"
                                                    ErrorMessage="&nbsp;Required&nbsp;"
                                                    CssClass="alert-danger" />
                                            </div>
                                        </div>
                                    </div>
                               
                                <button id="RequestBooking" runat="server" class="btn btn-lg btn-primary btn-block" type="button" onserverclick="RequestBooking_Click" validationgroup="RequestBookingValidationGroup">
                                    Request Booking
                                </button>
                            </ContentTemplate>
                        </asp:UpdatePanel>

        </div>
</form>

您可以启用或禁用(enabled=true 或 enabled=false)回传中所需的字段验证器(and/or 在标记或页面加载事件中)。相信它默认启用,这就是它被触发的原因。

@JobesK 感谢 Enabled=false 提示的帮助:)

我在标记中禁用了验证器,然后在提交按钮的 javascript 中启用了它们...

                ValidatorEnable(document.getElementById('<%=FreelancerRequired.ClientID%>'), true);
                                        ValidatorEnable(document.getElementById('<%=ServiceRequired.ClientID%>'), true);
                                        Page_ClientValidate("RequestBookingValidationGroup");