处理Webform中客户端验证错误的正确方式或方法是什么?

时间:2019-04-17 18:23:19

标签: javascript asp.net requiredfieldvalidator

当前,我正在一个基于ASP.NET Webforms的项目中。我在客户端应用了一些表单验证,但是我发现RequiredFieldValidator控件的行为很奇怪。查看此用户界面:

enter image description here

标记为必填的所有字段,并将RequiredFieldValidator附加到所有控件(活动复选框除外)。现在,当我按下提交按钮时,只有供应商下拉验证器向我显示错误。如下所示:

enter image description here

然后我从供应商下拉菜单中选择一些值,然后再次单击我的提交按钮:

enter image description here

现在,奇怪的是从供应商下拉列表中选择了值,所有控件都进行了验证,并向我显示了验证错误。从供应商下拉列表中选择一些值后,我的其他控件将得到验证。如果我从页面上删除下拉菜单,则所有控件都将生效。当我在页面上放置下拉列表并在其上附加了requiredfieldvalidator时,就会发生此问题。为什么当我单击“提交”按钮时,控件一次都无法验证。为什么其他控制取决于供应商下拉值的选择?有什么线索我在做什么错吗?

这是我的ASPX代码:

<div class="row">
    <asp:UpdatePanel
        runat="server">
        <ContentTemplate>
            <div class="col-md-4 ah-top-bottom-margin9px col-md-offset-2">
                <div class="input-group ah-equal-textbox-size">
                    <div class="input-group-addon">
                        <table style="width: 100%;">
                            <tr>
                                <td>Catalogue Name
                                </td>
                                <td style="text-align: right;">
                                    <i class="fa fa-exclamation-triangle ah-fa-error" aria-hidden="true"></i>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:TextBox
                        ID="txtCatalogueName"
                        runat="server"
                        ClientIDMode="Static"
                        CssClass="form-control"></asp:TextBox>
                </div>
                <asp:RequiredFieldValidator
                    ID="rfvCatalogueName"
                    runat="server"
                    EnableViewState="False"
                    EnableClientScript="False"
                    ControlToValidate="txtCatalogueName"
                    ForeColor="Red"
                    ErrorMessage="Catalogue name must be enter."
                    Display="Dynamic"
                    ValidationGroup="CatalogueDataSave">
                </asp:RequiredFieldValidator>
            </div>
            <div class="col-md-4 ah-top-bottom-margin9px ah-dropdown-border-left-top-left-bottom-flat">
                <div class="input-group ah-equal-textbox-size">
                    <div class="input-group-addon">
                        <table style="width: 100%;">
                            <tr>
                                <td>Quotation Ref
                                </td>
                                <td style="text-align: right;">
                                    <i class="fa fa-exclamation-triangle ah-fa-error" aria-hidden="true"></i>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:TextBox
                        ID="txtQuotationRef"
                        runat="server"
                        ClientIDMode="Static"
                        CssClass="form-control"></asp:TextBox>
                </div>
                <asp:RequiredFieldValidator
                    ID="rfvQuotationRef"
                    runat="server"
                    EnableViewState="False"
                    EnableClientScript="False"
                    ControlToValidate="txtQuotationRef"
                    ForeColor="Red"
                    ErrorMessage="Quotation Ref must be enter."
                    Display="Dynamic"
                    ValidationGroup="CatalogueDataSave">
                </asp:RequiredFieldValidator>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<div class="row">
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <div class="col-md-4 ah-top-bottom-margin9px col-md-offset-2">
                <div class="input-group ah-equal-textbox-size">
                    <div class="input-group-addon">
                        <table style="width: 100%;">
                            <tr>
                                <td>Catalogue Effective 
                                </td>
                                <td style="text-align: right;">
                                    <i class="fa fa-exclamation-triangle ah-fa-error" aria-hidden="true"></i>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:TextBox
                        ID="txtEffectiveDate"
                        runat="server"
                        CssClass="form-control"></asp:TextBox>
                    <span class="input-group-btn">
                        <asp:LinkButton ID="btnEffectiveCalendar"
                            runat="server"
                            CssClass="btn btn-default"
                            Style="border-top-left-radius: 0; border-bottom-left-radius: 0;"
                            ClientIDMode="AutoID">
                                            <i class="fa fa-calendar" aria-hidden="true"></i>
                        </asp:LinkButton>
                    </span>
                    <ajaxToolkit:CalendarExtender ID="ceEffectiveDate" CssClass="AH_Calendar" runat="server"
                        TargetControlID="txtEffectiveDate" Format="dd/MM/yyyy" PopupButtonID="btnEffectiveCalendar"></ajaxToolkit:CalendarExtender>
                </div>
                <asp:RequiredFieldValidator
                    ID="rfvEffectiveDate"
                    runat="server"
                    EnableViewState="False"
                    EnableClientScript="False"
                    ControlToValidate="txtEffectiveDate"
                    ForeColor="Red"
                    ErrorMessage="EffectiveDate must be enter."
                    Display="Dynamic"
                    ValidationGroup="CatalogueDataSave">
                </asp:RequiredFieldValidator>
            </div>
            <div class="col-md-4 ah-top-bottom-margin9px">
                <div class="input-group ah-equal-textbox-size">
                    <div class="input-group-addon">
                        <table style="width: 100%;">
                            <tr>
                                <td>Catalogue Expiry 
                                </td>
                                <td style="text-align: right;">
                                    <i class="fa fa-exclamation-triangle ah-fa-error" aria-hidden="true"></i>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:TextBox
                        ID="txtExpiryDate"
                        runat="server"
                        CssClass="form-control"></asp:TextBox>
                    <span class="input-group-btn">
                        <asp:LinkButton ID="btnExpiryCalendar"
                            runat="server"
                            CssClass="btn btn-default"
                            Style="border-top-left-radius: 0; border-bottom-left-radius: 0;"
                            ClientIDMode="AutoID">
                                            <i class="fa fa-calendar" aria-hidden="true"></i>
                        </asp:LinkButton>
                    </span>
                    <ajaxToolkit:CalendarExtender ID="ceExpiryDate" CssClass="AH_Calendar" runat="server"
                        TargetControlID="txtExpiryDate" Format="dd/MM/yyyy" PopupButtonID="btnExpiryCalendar"></ajaxToolkit:CalendarExtender>
                </div>
                <asp:RequiredFieldValidator
                    ID="rfvExpiryDate"
                    runat="server"
                    EnableViewState="False"
                    EnableClientScript="False"
                    ControlToValidate="txtExpiryDate"
                    ForeColor="Red"
                    ErrorMessage="Expiry must be enter."
                    Display="Dynamic"
                    ValidationGroup="CatalogueDataSave">
                </asp:RequiredFieldValidator>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<div class="row">
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <div class="col-md-4 ah-top-bottom-margin9px ah-dropdown-border-left-top-left-bottom-flat  col-md-offset-2">
                <div class="input-group ah-equal-textbox-size">
                    <div class="input-group-addon">
                        <table style="width: 100%;">
                            <tr>
                                <td>Select Supplier
                                </td>
                                <td style="text-align: right;">
                                    <i class="fa fa-exclamation-triangle ah-fa-error" aria-hidden="true"></i>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:DropDownList
                        ID="ddlSupplierList"
                        runat="server"
                        CssClass="form-control chosen-select"
                        Style="width: 280px;">
                    </asp:DropDownList>
                </div>
                <asp:RequiredFieldValidator
                    ID="rfvSupplierList"
                    runat="server"
                    EnableViewState="False"
                    ControlToValidate="ddlSupplierList"
                    ForeColor="Red"
                    ErrorMessage="Supplier must be selected."
                    ValidationGroup="CatalogueDataSave"
                    Display="Dynamic">
                </asp:RequiredFieldValidator>
            </div>
            <div class="col-md-4 ah-top-bottom-margin9px ah-dropdown-border-left-top-left-bottom-flat">
                <div class="input-group ah-equal-textbox-size">
                    <asp:UpdatePanel runat="server">
                        <ContentTemplate>
                            <div class="ah-active">
                                <span>Active</span>
                                <span>
                                    <asp:CheckBox
                                        ID="chkActive"
                                        runat="server"
                                        Checked="false" />
                                </span>
                            </div>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<div class="row">
    <div class="col-md-12">
        <div runat="server" class="panel panel-default">
            <div class="panel-body">
                <div class="text-center">
                    <asp:UpdatePanel
                        runat="server">
                        <ContentTemplate>
                            <asp:Button
                                ID="btnSaveItem"
                                runat="server"
                                Text="Save"
                                ValidationGroup="CatalogueDataSave"
                                CssClass="btn btn-primary"
                                OnClick="btnSaveItem_Click" />
                            <asp:Button
                                ID="btnReset"
                                runat="server"
                                Text="Reset"
                                CssClass="btn btn-default"
                                OnClick="btnReset_Click" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

除验证者rfvSupplierList之外,我所有人都有EnableClientScript="False"。因此,其余的仅用于服务器验证,而最后一个也用于客户端验证。因此,当您按下按钮时,它只会验证ddlSupplierList并阻止回发。

我建议从所有验证器中删除EnableClientScript="False",以便你们都进行客户端和服务器端验证。