CustomValidator not being called!

jenn5175

Freshman
Joined
Apr 4, 2002
Messages
35
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
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
 
First... make sure your control isn't Invisible (Visible to false).
Being invisible doesn't render the control and doesn't execute it.

Second... I put a textbox, a custom validator and a button. I enter the ServerValidate event. I put Response.Write(args.Value) in it.
And when I click on the button... it work.

Third... I put another set of control (another textbox and another customvalidator) and it worked for both. (At least... it enter in the ServerValidate Function)

I tried it with ASP.NET with C#. It worked on my side.

I went through the IDE to make my ServerValidate Function and it worked perfectly.

I don't know what is your problem.
 
I guess what I am looking for is someone to cut and paste the code I provided and try my exact code themselves to let me know if it does or doesn't work. I don't know what is wrong - in my head it "should" work - so any suggestions are welcome. Like I said, I have cut and pasted many examples and all work fine so I just need to find out what is wrong with MY code. Thanks,

Jenn
 
Not sure what the problem is. I just created a new project and pasted the html straight into the webform1.aspx replacing all the original.
I then pasted the vb code into the code behind leaving the existing code alone.

Ran web pagge and entered a prime number of 4 and a company name of test.
Both validators displayed their errors.
Changed the prime number to 1 and only the company validator displayed.
I tried this on a VS.Net 2002 running under XP.
 
I tried it on VS.Net 2003 Professional under Windows 2000 Professional.
But it was actually "building" the problem so it look just like you (didn't copy anything) but was identical at the end. And it worked.

Maybe their's a problem between IIS and VS.NET
 
Thank you guys for testing this for me! I think I realize what my problem is. It was my own lack of knowledge. I was assuming the validator was always called on submit. But it turns out it is called only when there is a value in the textbox. So during my testing I was just hitting submit with a blank textbox. Duh!

However, this brings a new issue that maybe you guys can help me brainstorm around. What I was going to use the customvalidator for was a senerio such as this:

I have a dropdown of company names. The 0 index of the listbox says "New Company". Then there is a textbox for them to type a new company name (if they don't like any of the dropdown choices). I wanted my validator to check if the index of the listbox was set to zero and the text box was blank to display an error. But looks like CustomValidator isn't the way to go because it won't fire if the new company name textbox is blank. I was looking into CompareValidators but don't think they have the flexibility I need. And can't use RequiredValidator because I only want the field required if the dropdown index is 0 on a seperate control.

The way I see it my only option is to do my error checking in my submit_click sub and not use any built in validators. Right?
Jenn
 
You might write your own function of validation but write it in the OnClick of your button. And if it's not valid you don't redirect to the next page (postback) and you show an error message with a Label.
 
I would do exactly as Arch4Angel says. Since you had asked for code to cut and paste though I thought I would show you what I do. First, create a label that is set to visible = "False" with the error message you want to display. Add this code to your button_click event.

Dim blnGoAhead as Boolean
blnGoAhead = True

If DropDownList.SelectedIndex = 0 AndAlso txtCompanyName = "" Then
LabelCompanyValidator.Visible = True
blnGoAhead = False
End If

If blnGoAhead = False Then
'Don't do anything. Just let the error be displayed.
Else
'Execute happy code here
End If
 
Back
Top