need to have onsubmit event fire after server-side validation.

U

usl2222

Hi folks,

I appreciate any assistance in the following problem:


I have a form with a bunch of dynamic controls on it. All the controls
are dynamically generated on a server, including all the validators.

The user enters the data, presses OK. My OK button is dynamically
generated as well, with some code-behind logic in
it's click event. CausesValidation property is set to false, since all
my validators are dynamic anyways,
and I do all my validation on a server. In the beginning of an
OK_click event, I call page validate explicitly:
Page.Validate();
if (!IsValid) return;

So far, so good. Now, in the beginning of my OK logic, I need to call
some javascript function, that will take some parameters,
call Modal Dialog and return some parameters to the server.

I attached this function to form OnSubmit event:
<form id="Form1" method="post" runat="server" onsubmit="test()">

Everything is great, I'm able to call the function that calls modal
dialog, grab a return value, set it
to a hidden field, and pick it up on a server.

The only one thing that I cannot figure out how to do, is: I want my
function to be called only AFTER the
validation, and only if validation is correct. I cannot figure out how
it do it, and if its possible.

My function keeps getting called before the validation.

I searched on the internet about this problem, and it seems like
people who had it and successfully resolved
it, all had client-side validation. But I have a server-side
validation.

I tried to mess with different things, like putting the javascript
function call in ResponseWrite, or
registering it with RegisterClientScript and
then calling it explicitly after Page.Validate(), but it doesn't work
anyway, it's even worse, because, now
javascript is executed in the very beginning of the form, before all
the controls are created, so I don't
have my hidden field control yet, to pass the information to the
server.

Is there any way? I appreciate your time, folks.
(e-mail address removed)
 
E

Earl Teigrob

Page.RegisterStartupScript puts the javascirpt at the end of the page, after
all controls have loaded. I use this to execute javascript after a
validation event is ok. You can then access the values of the controls.

Earl
 
P

Peter Blum

I agree with Earl's solution. Let me give you some background because you
are stating things that seem incorrect.

1. There is no way to call javascript from within your server side code.
2. If you have stopped client-side validation through
CausesValidation=false, the button will not generate code that runs
validation. However, the <form onsubmit= > statement will still create some
code that is used by validation to determine if the page submits. The
button's code sets a global variable Page_IsValid. When false, onsubmit is
stopped.
3. The order of events as you've described them makes no sense. If
Page.Validate() is called first, then the page had to be submittted (it gets
passed the onsubmit= attribute) to the server for processing. Yet, you are
concerned about your javascript occurring after. Your javascript will not
run until the page reloads on the client, long after the server is done
generating it. If you place is in the onsubmit attribute, it will only run
when a submit button is clicked.

As a result, Earl has you add the Javascript to the page in a location where
it will run immediately after the page is loaded into the browser.

This page design is not that easy to manage, as you can see. Most users
redirect to a new page after validation to handle the extra elements that
your modal dialog would cover. They would pass along values using the
QueryString or ViewState (this needs you to use Server.Transfer instead of
Response.Redirect). Good luck in your efforts to implement your design. Let
us know the results.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
B

bruce barker

that won't work either. the only way to call javascript from the server, is
to send a page back to the client that execute the javascript, then the
javascript auto posts the page back. the second postback will be a different
page request.

-- bruce (sqlwork.com)
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top