Click here to Skip to main content
15,898,996 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I have written a script like this:

XML
<script type="text/javascript">
function show_confirm()
{
    var r=confirm("Press a button");
    if (r==true)
    {
        window.location="Default.aspx";
    }
    else
    {
        alert("You pressed Cancel!");
    }
}
</script>

I have an asp:Button Calling that Function:
XML
<asp:Button ID="btnExit" Text="Exit"  OnClientClick="show_confirm()" runat="server"  />

If i Click OK it doesn't redirect to the specified Page.

Please help me for this.

Thanks
S.Priyan
Posted
Updated 15-Apr-11 21:13pm
v2
Comments
Jeffrey Enzo 16-Apr-11 3:10am    
Put your code in PRE tag, it's more readable for people

1 solution

I have seen such issue many a times. Change the code as below and it should work fine. I will highlight (in bold) the changed part.

JavaScript
function show_confirm()
    {
        var r = confirm("Press a button");
        if (r == true)
        {
            location.href = "Default.aspx";
            return false;
        }
        else
        {
            alert("You pressed Cancel!");
            return false; //this isn't necessary in your case but avoids post back.
        }
    }

And you will call the function as follows:
XML
<asp:Button ID="btnExit" Text="Exit" OnClientClick="return show_confirm();" runat="server"  />

Hope this helps!
 
Share this answer
 
Comments
Pratik Bhesaniya 16-Apr-11 6:28am    
Nice answer
Ankur\m/ 16-Apr-11 6:41am    
Thanks!
BTW nice answers should be voted up! :)
thatraja 16-Apr-11 10:43am    
I'll too. 5!
Ankur\m/ 18-Apr-11 1:46am    
Thanks Raja! BTW it's only you, so no too. :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900