Question about OnServerValidate

J

jm

I have this CustomValidator:

<asp:CustomValidator runat="server"
OnServerValidate="ValidateThis"
Display="Dynamic"
ControlToValidate="txtMyTextBox"
ErrorMessage="Not a valid entry"/>

On my server side scripts, I have:

sub ValidateThis(obj as Object, args as _
ServerValidateEventArgs)

if len(args.Value) < 8 then
exit sub <---will this stop the submit from firing?
else
submit() <---what argument
end if

end sub

Sub submit(Source As Object, e As EventArgs)
.... put stuff in a database too long to list
end sub

Okay, what I am trying to do is validate args.value (a textbox) and if
it meets or does not meet the criterion, then either stop everything
or submit to the database.

The problem is that no matter what I put in the ValidateThis sub, the
submit will always work. How do I use the OnServerValidate to stop
the entire page from submitting what's on the form. Also, if it does
pass my criterion, how do I get it too submit. Submit has the two
arguments, but I don't know what they want (got it from somewhere); it
does work. The form works, the database inserts data like I want. I
just need to know how to stop it from submitting based on invalid data
- and I don't want to use JavaScript. I can do that already. I want
to do it in .NET. Thanks for any help.
 
W

William F. Robertson, Jr.

You can't stop the page from submitting using server validate. Server
validate is called when the form has been submitting.

You might want to look at client side validation for this control ALSO, so
form will not be submitted. You will still need to have the server
validation on it because it isn't too difficult (I hear) to bypass client
side validation.

bill
 
M

Michael Pearson

In your ValidateThis() you have to do something like this

RequiredFieldValidator1.IsValid = false

That will cause the submit to fail, I think.

Michael
 
G

Greg Burns

Don't forget to add code like this at the beginning of the event that caused
the post back:

If Not Page.IsValid Exit Sub

Greg
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top