Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Confirm Postback of a page from Client End

0.00/5 (No votes)
13 Sep 2009 1  
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 c

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here