Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Validation Controls not stopping Form Post

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Kotuby - 05 Feb 2008 20:35 GMT
Hi all,

I was under the impression that the Validation Server Controls actually
perform validation on the Client and don't allow a form to Post if any of
the validations fail. Please correct me if I am wrong about that. If that is
the case then I will probably have to write my own validators in JavaScript
(which I thought that ASP.NET 2.0 would help me to avoid).

I have a form for filling out Email information which contains validation
controls. In fact I have 2 forms like that with the same validator controls
pointing to the same input fields. They just operate slightly differently
upon post.

One of them seems to work properly and the other one doesn't and I cant seem
to figure out why.

The Form1 that works (validates) properly is posted by the button as below:

<asp:Button runat="server" ID="btnSubmit" Text="Preview"
PostBackUrl="~/Common/EmailList.aspx" ToolTip="Preview Email" />

This Form1 cross posts to another page which shows a preview of the HTML to
be sent in the email and then sends it when the user clicks a "Send" button.

The Form2 that doesn't work (such that it allows the email to be sent
without a subject or email addresses) is posted by the button control below:

<asp:Button runat="server" ID="btnSend" Text="Send" OnClientClick="return
SendMail();" OnClick="SendIt" CausesValidation="true" ></asp:Button>

OnClientClick calls a Javascript function that assembles the Body content
(actually HTML) and places it a hidden field before the form is posted. That
part works fine and the HTML email sends OK. But, the Validation controls
don't seem to work.

Form2  posts back to itself, where it then runs the "SendIt" method that
actually sends the email.
The other difference is the OnClientClick javascript that is called. This is
the form where validation does not operate properly, meaning the
OnClick="SendIt" method is fired even if the form fields do not pass the
validation tests.

Can anyone tell me why the Validation appears to work in one case and not
the other? I have triple checked that all validators are associated with the
correct controls. In fact Form2 was built by copying much of the code from
Form1.

Thanks for any responses.
George Ter-Saakov - 05 Feb 2008 21:14 GMT
It's responsobillity of the code to check if page is valid befor doing any
action....

So your Button_OnClick code should have folowing code as first lines....

if( !this.IsValid)
   return;
........

George

> Hi all,
>
[quoted text clipped - 47 lines]
>
> Thanks for any responses.
John Kotuby - 07 Feb 2008 17:40 GMT
George,

Thanks for reminding me of the importance of checking the validity of the
submitted form at the Server, should the user bypass any JavaScript or
otherwise client-side validation. I have implemented your suggestion.

It did solve the problem of the Email being sent with invalid information,
but it required a PostBack and a complete form life-cycle, including another
trip to the database to gather (again) necessary display elements.

See my response to Mark Rae for the final solution, which I hope will help
others who might run across a similar situation.

Thanks again... John

> It's responsobillity of the code to check if page is valid befor doing any
> action....
[quoted text clipped - 58 lines]
>>
>> Thanks for any responses.
Mark Rae [MVP] - 05 Feb 2008 21:21 GMT
> I was under the impression that the Validation Server Controls actually
> perform validation on the Client and don't allow a form to Post if any of
> the validations fail. Please correct me if I am wrong about that.

http://msdn2.microsoft.com/en-us/library/yb52a4x0.aspx

> I will probably have to write my own validators in JavaScript (which I
> thought that ASP.NET 2.0 would help me to avoid).

FWIW, I never ever use the Validation controls...

I have a whole suite of client-side JavaScript validation routines which I
find give me much more control over the entire client-side validation
process...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

John Kotuby - 07 Feb 2008 17:59 GMT
Many thanks Mark,

The link to the explanation of the client-side workings of the Validation
Server controls finally led me to a solution.
I had placed a "filter" on the help searches in MSDN for VS2005 so it only
returned articles for "Web Development".
Apparently, whoever decides what the filters do made it difficult for me to
find the article you pointed me to. From now on I leave the Help
"unfiltered".

Anyway, it was my  OnClientClick="return SendMail();" that turned out ot be
the culprit.
It was being called right before the Microsoft generated
DoPostBackWithOptions client-side call, which of course runs the client-side
validation routines. The form was being submitted without client-side
validation, thus underscoring the need for Server-side form validation at
all times.

I moved the call to my function "SendMail();" from the Button OnClientClick
event to the Form onSubmit event so that it gets called only if client-side
validation is successful.

FWIW -- I am now well on my way to completing my own JavaScript validation
library, thanks to a handy tool I downloaded -- RegexBuddy -- to help me
with Regular Expressions. I highly recommend the tool for only about $40.

Thanks again

>> I was under the impression that the Validation Server Controls actually
>> perform validation on the Client and don't allow a form to Post if any of
[quoted text clipped - 10 lines]
> find give me much more control over the entire client-side validation
> process...
Mark Rae [MVP] - 07 Feb 2008 18:01 GMT
> FWIW -- I am now well on my way to completing my own JavaScript validation
> library

Glad to hear it!

Once you have it the way you like it, you'll never use client-side
Validation Controls again...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Scott Roberts - 05 Feb 2008 21:23 GMT
> Hi all,
>
[quoted text clipped - 47 lines]
>
> Thanks for any responses.

It sounds like you only want "SendMail()" to run if the page is valid. Try
this link:

http://forums.asp.net/p/1011848/1354154.aspx
John Kotuby - 07 Feb 2008 17:46 GMT
Scott,

Thanks for your suggestion. I implemented it in a slightly different manner.
Instead of what I had origianlly done:

OnClientClick="return SendMail();"

I placed that call in the form onSubmit event instead, so that I could still
gather up the on-screen InnerHtml right before the post. See my response to
Mark Rae for more info on how I solved the problem.

Essentially, though, by making the call to my JavaScript function and
returning "true" I was client-hijacking the Microsoft generated
DoPostbackWithOptions call that followed my Javascript as the 2nd command in
the HTML onClick event...such that the form was submitted without
client-side Validation ever being run.

>> Hi all,
>>
[quoted text clipped - 52 lines]
>
> http://forums.asp.net/p/1011848/1354154.aspx

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.