firing order fo clientSide script

P

Papanii

Hello Guys,
I have a little problem that i need some ideas as to how to
solve. I have a form that sends some data to the server once filled. Now,
for this form i am using asp.net validation controls to ensure certain
fields are filled and also have the correct data format. What i want to
have happen is once the form has been validated and the submit button
clicked, i want a javascript function to fire before continuing with the
server side code. Now, i thought that using the submit button's
onclientClick property would work for this however, this fires the
javascript function regardless of whether or not the validation is
successful. So i decided to add the
Page.ClientScript.RegisterClientScriptBlock at the begining of the server
side code however this only seems to fire after the server side code
completes. I am assuming it has something to do with the switching of
running thread within the process however i am not sure. Does anyone have an
idea as to how i can get the javascript code to run only after validation is
successful but before the server side code runs.


Thanks
--Papanii
 
T

Teemu Keiski

Hi,

that means you'd rely only on the client-side validation(since you want the
msg to appear before the postback occurs and before server-.side validation
whatsoever). If that's acceptable, you could use RegisterOnSubmitStatement

Page.ClientScript.RegisterOnSubmitStatement(Me.GetType(), "validcheck",
"if(!Page_IsValid){alert('Validation failed');return false;}")

Run it in Page_Load (or at PreRender at the latest) so that it's registered
by the time page is rendered

Of course, you could use this trick with buttons too, since it uses
client-side Page_IsValid variable (part of client validation library) to
check if client-side validation passed or not.

Page.ClientScript.RegisterClientScriptBlock is meant to generate script
calls from server-side code, and as you can understand they run at the
client...which gets it when server-side code is done and page is rendered
 
B

bruce barker

there are three spots that asp.net supports adding script:

RegisterClientScriptBlock - javascript appears after <form> (before form
element defined.

RegisterStartupScript - javascript appears just before </form> (after
form elements defined.

RegisterOnSubmitStatement - javascript eval'd after validation, before
form.submit

-- bruce (sqlwork.com)
 
P

Papanii

Thanks a lot guys, i totally forgot about the RegisterOnSubmitStatement
method..

--Papanii
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top