Adding customised javascript on Submit button before postback?

Hi there,

Created a webform ( c#)

<form id=Form1 method=post runat="server">

with the following submit button

<asp:Button id=searchButton runat="server" Text="Search"></asp:Button>

Basically when you click the searchButton (submit) button then it does a postback, everything is working but what I want to do is run some javascript prior to doing the postback.

The javascript (clientside) will check some values of textboxes etc and if they are valid then the POSTBACK will continue otherwise I will display to the user "Sorry please check the form" and postback is cancelled.

Wow! I don't even know where to start on this, can anybody help?

I have written the javascript clientside code, it is in a function called "CheckThisNow"

Any ideas of where to start on this? Appreciated any input anyone has, or any documents you can point me in the direction of.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi ianinspain,

Sub Page_Load
    searchButton.Attributes.Add("onclick", " return CheckThisNow();")
End Sub

this will add the client event to the button and if the CheckThisNow return false, the submit will not be proceeded


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ianinspainAuthor Commented:
Hi mmarinov,

Cool! as simple as that... going to give it a try..

I presume i just use this to add to a submit button, all my javascript functions still need to be embedded within the HTML between tags as normal?

yes, you have to put your javascript code in the HTML

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.