Thursday, January 5, 2023

Custom Validator ASP.NET C# Server side and client side

Server side

  protected void btnAddNew_Click(object sender, EventArgs e)

        {

            if (CustomValidator1.IsValid == true)

            { // button click logic here

}

}


 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)

        {

            {

                args.IsValid = true;

                if (int.Parse(txtOpenVacancy.Text) > int.Parse(txtSanctionedStrength.Text))

                {

                    args.IsValid = false;


                    ((CustomValidator)source).IsValid = false;

                    CustomValidator1.ErrorMessage = "Open Vacancy is less than or equal to sanction strength";

                    return;

                }

                else

                {

                    args.IsValid = true;

                }

            }

        }


aspx page code

<asp:TextBox runat="server" ID="txtOpenVacancy" TextMode="Number" CssClass="col-md-12 form-control input-sm" Visible="false" ValidationGroup="addrecord" />

<asp:CustomValidator ID="CustomValidator1" runat="server"  Display="Dynamic" ForeColor="Red" ErrorMessage="" ValidationGroup="addrecord" SetFocusOnError="true" ControlToValidate="txtOpenVacancy" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>


Client side


https://stackoverflow.com/questions/29340294/button-click-fires-before-custom-validation-occurs

No comments:

Post a Comment