I have a peculiar problem here and I can\'t by my life figure out what the solution is. Note that the following code is not dynamically created, but just immediatel
The accepted answer is not perfect. If you do not use type="button"
, the web page will do postback even you have clicked cancel. The correct and easiest way is to take advantage of short-circuit evaluation and do this hack: replace ;
with && !
, like below.
<button runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?') && !">
The output will look like this:
<button id="btnSubmit"
onclick="return confirm('Sure?') && ! __doPostBack('btnSubmit','')">
It gives correct return value because true && !undefined
will return true
and undefined
will be evaluated and false && !undefined
will return false
and undefined
will NOT be evaluated which is exactly what we want.
Front Site
<button id="submit1" runat="server"
onclick="if(confirm('Sure?')) { } else{ return false} ;"
onserverclick="submit_ServerClick" >save</button>
Back Site
protected void submit_ServerClick(object sender, EventArgs e)
{
}