By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
us*****@yahoo.com
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
<us*****@yahoo.com> wrote in message
news:27**************************@posting.google.c om...
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.
us*****@yahoo.com

Nov 18 '05 #2

P: n/a
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

<us*****@yahoo.com> wrote in message
news:27**************************@posting.google.c om...
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.
us*****@yahoo.com

Nov 18 '05 #3

P: n/a
Earl, Peter,

thanks for answering.
I understood your explanation, Peter. I got confused at first, I
guess. I guess i had no idea what I
was doing.
I'm gonna use your advice
and try to implement Server.Transfer method. Actually, I will do
Server.Execute, because one thing
I forgot to mention in my email, is that I need to call Javascript,
not only AFTER the validation,
but BEFORE the code that follows validation.

So I have in on_click event:
{
<some server code>
Page.Validate();
if (!IsValid) return;
<have to call javascript right here>
<some server code>
}
So I have to call javascript and resume right at the point i left off.
After your explanation I
understood that there's no way in freeking hell to do it, like I was
trying to do.
I could manage to not call javascript if validation failed, by setting
some hidden field variable
to false, and reading it in a javascript, but if the validation
succeeded, there's no way I could
call javascript right after the validation, and before the rest of the
code, because the entire
server logic will be executing first,and only then, the client will
run javascript.

So that seems the only option for me, to use ServerExecute, to go to
some aspx page that will, in turn,
call the javascript.

Many thanks to both of you, again,
us*****@yahoo.com
Nov 18 '05 #4

P: n/a
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)


<us*****@yahoo.com> wrote in message
news:27**************************@posting.google.c om...
Earl, Peter,

thanks for answering.
I understood your explanation, Peter. I got confused at first, I
guess. I guess i had no idea what I
was doing.
I'm gonna use your advice
and try to implement Server.Transfer method. Actually, I will do
Server.Execute, because one thing
I forgot to mention in my email, is that I need to call Javascript,
not only AFTER the validation,
but BEFORE the code that follows validation.

So I have in on_click event:
{
<some server code>
Page.Validate();
if (!IsValid) return;
<have to call javascript right here>
<some server code>
}
So I have to call javascript and resume right at the point i left off.
After your explanation I
understood that there's no way in freeking hell to do it, like I was
trying to do.
I could manage to not call javascript if validation failed, by setting
some hidden field variable
to false, and reading it in a javascript, but if the validation
succeeded, there's no way I could
call javascript right after the validation, and before the rest of the
code, because the entire
server logic will be executing first,and only then, the client will
run javascript.

So that seems the only option for me, to use ServerExecute, to go to
some aspx page that will, in turn,
call the javascript.

Many thanks to both of you, again,
us*****@yahoo.com

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.