I am so frusterated. I am trying to learn how to use the Custom Validator control since this is my first ASP.NET project. So I have cut and pasted a ton of examples and they all work, but when I try it myself the OnServerValidate function is never even called (verified using break points)! So I took a very simple example from the web:
IN WEBFORM1.ASPX
IN WEBFORM1.ASPX.VB
This worked. So then I pasted my control in there also to validate so the pages became:
IN WEBFORM1.ASPX
IN WEBFORM1.ASPX.VB
The first example of validation still works great. But I can never get validateCompanyName to be called! Why?
I am at a loss as to what to try next. I have cut and pasted the working code and just replaced variables even and still nothing. HELP!
Jenn
IN WEBFORM1.ASPX
Code:
:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="MediaLibrary.WebForm1"%>
<form method="post" runat="server">
Enter your favorite prime number:
<asp:textbox id="txtPrimeNumber" runat="server"></asp:textbox>
<asp:customvalidator id="custPrimeCheck" runat="server" ControlToValidate="txtPrimeNumber" ErrorMessage="Invalid Prime Number"
OnServerValidate="PrimeNumberCheck"></asp:customvalidator><br>
<asp:button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:button></form>
IN WEBFORM1.ASPX.VB
Code:
Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsValid Then
Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _
" is, indeed, a good prime number.</i></font>")
Else
Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _
" is <b>not</b> a prime number.</i></font>")
End If
End Sub
Sub PrimeNumberCheck(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
Dim iPrime As Integer = CInt(args.Value), iLoop As Integer, _
iSqrt As Integer = CInt(Math.Sqrt(iPrime))
For iLoop = 2 To iSqrt
If iPrime Mod iLoop = 0 Then
args.IsValid = False
Exit Sub
End If
Next
args.IsValid = True
End Sub
This worked. So then I pasted my control in there also to validate so the pages became:
IN WEBFORM1.ASPX
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="MediaLibrary.WebForm1"%>
<form method="post" runat="server">
Enter your favorite prime number:
<asp:textbox id="txtPrimeNumber" runat="server"></asp:textbox>
<asp:customvalidator id="custPrimeCheck" runat="server" ControlToValidate="txtPrimeNumber" ErrorMessage="Invalid Prime Number"
OnServerValidate="PrimeNumberCheck"></asp:customvalidator><br>
Company name:
<asp:textbox id="txtNewCompanyName" runat="server"></asp:textbox>
<asp:customvalidator id="valCompanyName" runat="server" ControlToValidate="txtNewCompanyName" ErrorMessage="INVALID COMPANY NAME"
OnServerValidate="validateCompanyName"></asp:customvalidator><br>
<asp:button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:button></form>
IN WEBFORM1.ASPX.VB
Code:
Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsValid Then
Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _
" is, indeed, a good prime number.</i></font>")
Else
Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _
" is <b>not</b> a prime number.</i></font>")
End If
End Sub
Sub PrimeNumberCheck(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
Dim iPrime As Integer = CInt(args.Value), iLoop As Integer, _
iSqrt As Integer = CInt(Math.Sqrt(iPrime))
For iLoop = 2 To iSqrt
If iPrime Mod iLoop = 0 Then
args.IsValid = False
Exit Sub
End If
Next
args.IsValid = True
End Sub
Sub validateCompanyName(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
args.IsValid = False
End Sub
The first example of validation still works great. But I can never get validateCompanyName to be called! Why?
I am at a loss as to what to try next. I have cut and pasted the working code and just replaced variables even and still nothing. HELP!
Jenn