Stop page postback from javascript

Posted on 2006-10-27
Last Modified: 2011-10-03
Here is my scenario

I have a asp:button and that has a server side click event. The page_load binds a javascript function as attribute for ‘onclick’. The javascript function has does some client side calculations and displays confirm message box. How do I stop the page from posting if the user hits cancel in the confirm box?

-- html
<asp:Button ID="btnSave" runat="server" CssClass="buttonpri" Text="Submit"></asp:Button>

function doSubmit() {
// do client side calculations

var submit = confirm('<display calculated value>. Do you want to submit?')
if (submit)
//do page __postback
//dont submit


--Page_Load event
btnSave.Attributes.Add("onclick", "javascript:doSubmit();")

-- onclick event
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
End sub
Question by:sansoftura

Accepted Solution

avanishp earned 250 total points
ID: 17824769

btnSave.Attributes.Add("onclick", "return doSubmit();")

and in javascript function

function doSubmit() {
// do client side calculations

var submit = confirm('<display calculated value>. Do you want to submit?')
if (submit)
 return true
 return false


Author Comment

ID: 17825657
Thanks! it Worked.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

19 Experts available now in Live!

Get 1:1 Help Now