0

I have the following code:

<asp:TableCell>
    <asp:Label Text="Date of Birth:" Runat="server" cssClass="EditLabel" ID="Label3"/>                                              
</asp:TableCell>
<asp:TableCell>
    <asp:Textbox id="txtDOB" Runat="server" EnableViewState="True"/>
    <asp:CustomValidator ControlToValidate="txtDOB" ErrorMessage="Date of Birth is not Valid" Runat="server" OnServerValidate="CheckDOB">*</asp:CustomValidator>                        
</asp:TableCell>
<asp:TableCell>

Function is defined as:

Protected Sub CheckDOB(ByVal source As Object, ByVal args As ServerValidateEventArgs)

  ....

End Sub

When I submit the code function is never hit. What is missing here?

7
  • Can you show the markup for the button (assuming that is how the form is being submitted)? Commented Jul 23, 2013 at 15:38
  • enter some text in the txtDOB and check Commented Jul 23, 2013 at 15:40
  • <asp:TableRow> <asp:TableCell> <asp:Button Text="Add Participant" Runat="server" ID="btnAddParticipant"/> </asp:TableCell> </asp:TableRow> Commented Jul 23, 2013 at 15:43
  • I have 2 Required field validators that work properly when button is pressed. Commented Jul 23, 2013 at 15:43
  • I can enter any text in txtDOB without any errors. the Main problem is when I put a breakpoint at Protected Sub CheckDOB, it will never hit. Commented Jul 23, 2013 at 15:45

1 Answer 1

1

Try redefining the handler. Remove OnServerValidate="CheckDOB" from Custom ASPX validator markup and define handler in VB like this:

Protected Sub CheckDOB(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate

End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

OK, Adding the handler directly make it work. I have no idea why for this case it needed a handler explicitly. Now IF I have valid data for First Name and Last Name and press the button then CheckDOB is called. You get a +1 here. But I need to know why some controls like button do not need an explicit event handler.
Your code worked without explicit handler in my environment. but then I simple copy/pasted your code from here. I guess your page's markup is a lot more complex, so, something must be interfering. As a rule of the thumb in VB.NET code I always use Handles approach.
By the way, if you want to handle 'no data' scenario - add ValidateEmptyText="true" to your Custom Validator attributes
You said my code worked when you tried it. no my code is not complex. Just try this also : Add a textbox name it Fname and add a requiredfiled validator for it. Now see if Customvalidator is still working. I appreciate your time for testing this.
I don't see a ValidateEmptyText attribute. I Guess it is not part of framework1.1 which I am limited to, but that's OK.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.