Adding customised javascript on Submit button before postback?

Posted on 2004-11-18
Last Modified: 2008-02-26
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.

Question by:ianinspain
    LVL 28

    Accepted Solution

    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


    Author Comment

    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?

    LVL 28

    Expert Comment

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now