Search This Blog

2009-05-19

Javascript YES NO Confirm Dialog

In few cases we need to show yes now popup instead of OK Cancel confirm dialog box.



You can do it by the following code.

Write the following code in the head section of your HTML Page

<script language="javascript" type="text/javascript">
/*@cc_on @*/
/*@if (@_win32 && @_jscript_version>=5)
function window.confirm(str)
{
execScript('n = msgbox("'+str+'","4132")', "vbscript");
return(n == 6);
}
@end @*/
</script>

Call the function from the onClientClick event of your button

<asp:Button ID="btnOk" runat="server" OnClientClick="javascript:return confirm('Are you sure to proceed?');" OnClick="btnOk_Click" Text="OK" />

Write the btnOk_Click event in your server side code
protected void btnOk_Click(object sender, EventArgs e)
{
Response.Write("User Click Yes");
}

Now run your application,click the "OK" button.You can see a Yes NO Dialog Box,if the user click yes the control will go to the server side method,if the user click no,the Server side code will not execute

You can change the Dialog as OK Cancel by changing the value 4132 to 4129 in the statement execScript('n = msgbox("'+str+'","4132")', "vbscript");



For Abort Rety Ignore change the value to 4130



For yes no cancel change the value to 4131



For Retry Cancel ,Change the value to 4133



For OK Change the value to 4134


The @cc_on statement activates conditional compilation in the scripting engine.
It is strongly recommended that you use the @cc_on statement appear in a comment, so that browsers that do not support conditional compilation will accept your script as valid syntax:
//@cc_on
...
(remainder of script)
Alternatively, an @if or @set statement outside of a comment also activates conditional compilation.

1 comment:

Niidhiin said...

I want to use my custom title for this confirm window, instead of 'VBScript'. How can I do that