Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am not so familiar with java Script, 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!

share|improve this question

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
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 '12 at 8:03
<asp:Button ID="cancel" runat="server" Text="Cancel" CausesValidation="false" onClientClick="return checkValidation();"/> – mike Nov 16 '12 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 '12 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 '12 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 '12 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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