custom validation

S

Slim

I cant seem to get my custom validation to work. can anyone spot where i
have gone wrong

control
<asp:CustomValidator ID="checkHandicap" runat="server"
ControlToValidate="startHandicap" ErrorMessage="Handicap out of range"
OnServerValidate="checkHandicap_ServerValidate" ></asp:CustomValidator>



code behind


Protected Sub checkHandicap_ServerValidate(ByVal source As Object, ByVal
args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles
checkHandicap.ServerValidate

Dim ans As Integer = Integer.Parse(args.Value)

Try

If ans > -27 And ans <= 27 Then

args.IsValid = True

Exit Sub

End If

Catch exc As Exception

End Try

args.IsValid = False

End Sub
 
D

Dblood

Slim,

Are you getting an error message? What values are failing? I mocked up
an example using your code, but don't know how it's failing for you so
I don't really know how to help.

dkb
 
S

Slim

I get no error, but I get no return either,

from what tests I could do, the function was not firing
 
D

Dblood

Slim,

On the mockup I created, the event fired, and your code worked. I
don't see a reason why it would not work. I copied your control
declaration from the post, copied the code from the post. I dumped them
into a page, added the withevents declaration of the checkhandicap
variable, added a button (so that I could have the page postback) and
it worked.

There must be more to the problem. What is causing postback in your
app? Can you set a breakpoint, then run in debug and verify that the
checkHandicap_ServerValidate Sub is not being called? If not, add a
"response.write(args.IsValid)" at each of the conditions (where
args.IsValid is set to true or false) to ensure that the app is calling
the sub.

This is a weird one,

dkb
 
S

Slim

Dblood said:
Slim,

On the mockup I created, the event fired, and your code worked. I
don't see a reason why it would not work. I copied your control
declaration from the post, copied the code from the post. I dumped them
into a page, added the withevents declaration of the checkhandicap
variable,

where do i put the with events?


added a button (so that I could have the page postback) and
 
D

Dblood

What I mean is that when I copied your control (the text below) to an
aspx page, it didn't create the variable for me on the code-behind
page.

<asp:CustomValidator ID="checkHandicap" runat="server"
ControlToValidate="startHandicap" ErrorMessage="Handicap out of range"
OnServerValidate="checkHandicap_ServerValidate"></asp:CustomValidator>

So, I had to create that variable myself. Yours is probably already
there. Near the top of the code-behind view you'll see:

Protected WithEvents checkHandicap As CustomValidator

Any luck using the Response.write to see if the isvalid ever changes?
Can you debug the project and step through to see if the event is
firing?

dkb
 
S

Slim

No the response.write is not returning anything

and no where on my page or code behind is a WithEvents statement
 
D

Dblood

Slim,

Add this to your code behind then,

Protected WithEvents checkHandicap As _
System.Web.UI.WebControls.CustomValidator


and then try again.

dkb
 
K

Kronzucker

Dblood said:
Slim,

Add this to your code behind then,

Protected WithEvents checkHandicap As _
System.Web.UI.WebControls.CustomValidator


and then try again.

dkb


Hey Slim. If I understand you right, I had the same problem some time
ago on the client side. In my case args.Value didn't return anything.
The event was fired, but arg.Value was empty.

My solution was to adress the textbox directly throug something like:
((TextBox)Page.FindControl("startHandicap")).Text

MfG
Georg Fleischer
 
S

Slim

Dblood said:
Slim,

Add this to your code behind then,

Protected WithEvents checkHandicap As _
System.Web.UI.WebControls.CustomValidator


and then try again.

sorry took so long to reply

It tells me that it is already declared with events, yet its not on the
page.

I assume that VS2005 hides it from view.
 
S

Slim

I have since found that no events are working in the site except for page
load, they were working until recently?
 
S

Slim

Slim said:
I have since found that no events are working in the site except for page
load, they were working until recently?

sorry false alarm, its only that page, event a simple button on click event
will not work on the page.

I have recreated a new page with new code behind page, and when I copy html
and controls I have the same problems
 
S

Slim

Sorry to waste your time

I found that I had to make sure the all other required validation controls
were satisfied before custom validation would work.

after pulling my hair out for days, I calmed down took a deep breath and had
a logical look at my code,

thanks for your time
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top