Search code examples
javascriptasp.netrequiredfieldvalidator

How or What is the correct way to handle client side validation errors in webform?


Currently, I'm working on a project which is based on ASP.NET Webforms. I'm applying some forms validations on my client side but I just found strange behavior of RequiredFieldValidator control. See this UI:

enter image description here

All fields marked as required and attached RequiredFieldValidator to all controls, except active checkbox. Now when I press my submit button only supplier dropdown validator shows me error. Like below:

enter image description here

then I select some value from supplier drop down and again click on my submit button:

enter image description here

Now, the strange thing is after selecting the value from the supplier dropdown, all controls validate and shows me the validation error. After selecting some value from the supplier dropdown then my other controls are validating. If I remove my dropdown from my page then all controls validate. The issue occurs when I put dropdown on my page and attached the requiredfieldvalidator to it. why my controls are not validating once when I click on the submit button. Why other controls are dependent on the selection of supplier dropdown value? Is there is any clue what I'm doing wrong?

Here is my ASPX code:

<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>

Solution

  • I all but Validator rfvSupplierList you have EnableClientScript="False". Therefore the rest only does server validation while the last one does client side also. So when you press the button it only validates ddlSupplierList and blocks the PostBack.

    I would suggest removing EnableClientScript="False" from all validators so you both have client and server side validation.