Click here to Skip to main content

Confirm Postback of a page from Client End

It is a very common issue to handle automatic postbacks to controls. Here I am going to explain how we can handle autopostback of a button click using Javascript Event. Suppose you have a button : Now you want to occationally c
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

It is a very common issue to handle automatic postbacks to controls. Here I am going to explain how we can handle autopostback of a button click using Javascript Event.

Suppose you have a button :

<asp:button runat="server" onclick="btn_click" 
Id ="btnAutoPostBack" /> 

Now you want to occationally cancel the click event so that the page will only be posted back when you want. So add this code:

<asp:button runat="server" onclick="btn_click" 
Id ="btnAutoPostBack" onclientclick="javacript:return isValid();"/>

or Add the attribute onclick to the button in Page_Load :

btn.Attributes.Add("onclick", "javascript:return isValid()");

Now let us look what the function look like:

function isValid(){
   return confirm("Are you sure you want to postback?");
   //Returns true when click ok, otherwise false.
}


Thus when you click on Ok, the btn_click will get generated as the page is posted back, otherwise the page will remain intact.
here Javascript:return false; means we are disallowing the postback event of the button.

Posted 13 Sep '09


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. Christian Graus (1,114)
  2. SAKryukov (607)
  3. CRDave1988 (412)
  1. SAKryukov (10,433)
  2. Christian Graus (7,113)
  3. OriginalGriff (5,166)
  4. Abhinav S (4,550)
  5. thatraja (4,376)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --

Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 13 Sep 2009
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid