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>
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.
in the server code try the following procedure
Protected Sub CustomValidator1_ServerVal
Dim cv As CustomValidator = CType(source, CustomValidator)
Dim gvr As GridViewRow = cv.NamingContainer
Dim txt1 As TextBox = gvr.FindControl("txtApprov
(If condition validating txt1)
args.IsValid = False
(End If Condition)
End Sub