Why has my validation stopped working? [1/1]

A

Alan Silver

Hello,

I have been playing with some of the code in ASP.NET Unleashed, to try
and understand what's going on. I am getting on pretty well, but have
struck a problem. I was looking at samples for hiding panels, to do
multistep forms, and then at validation.

All was going well and I was getting the hang of validation, when it
just stopped working. I'm not sure what I did, and the code compiles and
runs fine, but the required fields can be left empty without an error.

I have included the code below. Sorry it's a bit long, but I'm not
fluent enough at it to work out which bits to chop out yet!!

Thanks for any help.

Alan
 
K

Karl Seguin

Alan:
You've set the EnableClientScript="false" in your validation controls, which
is all fine and good, but you'll probably be surprised to know that
server-side validation doesn't happen automatically like client-side does.

In your next-prev buttons (or in any buttons/events/place you want to
validate) you need to do:

void btnNextPage_Click( object s, EventArgs e ) {
Page.Validate();
if (Page.IsValid){
//your normal code here
}
}

Hope this helps,
Karl
 
A

Alan Silver

Alan:
You've set the EnableClientScript="false" in your validation controls, which
is all fine and good, but you'll probably be surprised to know that
server-side validation doesn't happen automatically like client-side does.

Ah ha, that would explain why it stopped working. I had only just added
that bit. Client-side validation wasn't happening anyway (possibly the
JS files were missing, but nothing happened) and I tried turning it off.
I did a couple of other changes at the same time and forgot that I had
changed this bit.

Please explain *why* this happens, I don't see the logic that says that
server-side validation only happens automatically when client-side
validation is enabled. Surely the two should be independent? Why does
turning off client-side also turn off server-side? More to the point,
can I have automatic server-side validation without having client-side?

Also, does server-side get turned off if I disable client-side for just
one control, or is it only when I disable it for all controls?
In your next-prev buttons (or in any buttons/events/place you want to
validate) you need to do:

void btnNextPage_Click( object s, EventArgs e ) {
Page.Validate();
if (Page.IsValid){
//your normal code here
}
}

Hope this helps,

Sure does, thanks.
 
K

Karl Seguin

Alan,
You can automatically get server-side validation by setting the
CausesValidation attribute to true in the button/linkbutton doing postback,
ala:

<asp:Button Text="Next Page >>" OnClick="btnNextPage_Click"
CausesValidation="True" Runat="Server" ID="Button1"/>

you still need to wrap your button codebehind code in a if
(Page.IsValide){ } which makes sense since the postback is still occuring
and you might wanna do something on the server side if it isn't...

Hope this helps,

Karl
 
A

Alan Silver

Thanks Karl, I think it helps!!

I think I'll leave client-side enabled for the minute and worry about
the major issues first. I am actually getting the hang of this slowly!

Thanks

a
 

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,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top