Avatar of nabeelmoeen
nabeelmoeen
 asked on

CustomValidator Control in GridView not firing even

HI,

I have a GridView with a CustomValidator control for one column and a RangeValidator control for another in Insert mode (textboxes to insert value).
the problem is, the RangeValidator control works fine, but OnServerValidate method of the Customer Validator control never fires.
The templateColumn definition for my column is given below.

Regards,
Nabeel
<FooterTemplate>
						                <asp:TextBox ID="txtApproverID" runat="server" Text='<%# Bind("ApproverID") %>' CausesValidation=true ValidationGroup="InsertValidationGroup"></asp:TextBox>
						                <br />
						                <asp:RequiredFieldValidator id="vdRequiredIDValidator" runat="server" ErrorMessage="Please provide a Approver ID." ControlToValidate="txtApproverID"
						                    ValidationGroup="InsertValidationGroup">*</asp:RequiredFieldValidator>
						                <asp:CustomValidator id="vdCustomIDValidator" runat="server" 
                                            ErrorMessage="The short name you entered is invalid." 
                                            ControlToValidate="txtApproverID" 
                                            OnServerValidate="ApproverName_ServerValidate" Display="Dynamic" 
                                            EnableClientScript="False" SetFocusOnError="True" ValidationGroup="InsertValidationGroup">*</asp:CustomValidator>
						            </FooterTemplate>
						        </asp:TemplateField>

Open in new window

Editors IDEsASP.NET

Avatar of undefined
Last Comment
nabeelmoeen

8/22/2022 - Mon
66866

In your customvalidator, remove the "ControlToValidate" tag and set ValidateEmptyText=True in the definition for the customvalidator
in the server code try the following procedure

 Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim cv As CustomValidator = CType(source, CustomValidator)
        Dim gvr As GridViewRow = cv.NamingContainer
        Dim txt1 As TextBox = gvr.FindControl("txtApproverID")
       (If condition validating txt1)
            args.IsValid = False
      (End If Condition)
    End Sub
nabeelmoeen

ASKER
the validation event still doesn't fire, even when I set the ValidateEmptyText = true
(btw, when is it supposed to fire if the ControlToValidate property is left empty?)
nabeelmoeen

ASKER
ok, got it to work by setting the "CausesValidation" of the insert button to true, but setting the IsValid argument to false in the code doesnt prevent the row from being added to the gridView.

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
66866

In your event for inserting row, check the page.isvalid value and only update the gridview if it's true
ASKER CERTIFIED SOLUTION
nabeelmoeen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.