0

I am not so familiar with JavaScript, for that reason I need your help and advice! I have the following code in my asp button when is clicked. When the confirm box is displayed the user has two choices either to select OK or Cancel. The following code works in both of cases either OK or Cancel.

 protected void cancel_Click(object sender, EventArgs e)
    {
        string url = "../../Default.aspx";
        
        ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "confirm('Data is not saved'); window.location.href = '" + url + "';", true);
    }

However, what I am trying to do is to perform an if/then/else statement using JavaScript inside ClientScript function, and I don't know the correct syntax of that. e.g what I am trying to do

ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "javascript:if(confirm('Data is not saved')== true) return {document.location.href = '../../Default.aspx'}; else {document.location.href = '../../Current.aspx'};", true);

Any advice would be appreciated!

1 Answer 1

1

Try the script before you add it server side, it easier to debug that way.

Here´s two ways to write the if statement;

if (confirm('Data is not saved')) {
 window.location.href = '../../Default.aspx';
} else {
 window.location.href = '../../Current.aspx';
}

or even;

window.location.href = confirm('Data is not saved') ?
    '../../Default.aspx' : '../../Current.aspx';

UPDATE

<asp:Button ID="cancel" runat="server" Text="Cancel" CausesValidation="false"
  onClientClick="window.location.href = confirm('Data is not saved') ? '../../Default.aspx' : '../../Current.aspx';"
/>

Also note that you should rather use window.location than document.location.

5
  • I tried to do that but it did not work...whereas the confirm box was displayed properly, when I pressed OK or Cancel nothing was happened(remain at the same page instead of redirection)
    – mike
    Nov 16, 2012 at 8:03
  • <asp:Button ID="cancel" runat="server" Text="Cancel" CausesValidation="false" onClientClick="return checkValidation();"/>
    – mike
    Nov 16, 2012 at 8:10
  • <script type="text/javascript"> function checkValidation() { if (confirm("Data is not saved") == true) { document.location.href = "google.com"; } else { document.location.href = "../../Default.aspx"; } } </script>
    – mike
    Nov 16, 2012 at 8:12
  • I posted you my button and the script..No redirections with this approach, either external or internal redirection, for that reason I am trying with my post, do you have any advice why is not working? thank you
    – mike
    Nov 16, 2012 at 8:14
  • You have tagged your question with javascript-library. What library are you referring to? I updated my answer with the ASP.NET button control.
    – Stefan
    Nov 16, 2012 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.