0

I use following code-behind for javascript pop up for conforming, it works well but it does redirect the user to TestPage.aspx always regardless of user selection whether Yes or No.

lblMsg.InnerHtml = @"
    <script type='text/javascript'>
        confirm('Do you want to continue?');
        window.location='TestPage.aspx?ID=" + Request.QueryString["ID"].ToString() + "&txtTest=" + Server.UrlEncode(txtTest.Text) + strSomeString + "'
    </script>";

Any idea?

And I use this, this time there is no pop up even.

lblMsg.InnerHtml = @"
    <script type='text/javascript'>
        confirm('Do you want to continue?');
        window.location='TestPage.aspx?ID=" + Request.QueryString["ID"].ToString() + "&txtTest=" + Server.UrlEncode(txtTest.Text) + strSomeString + "'; return false;
    </script>";
1
  • Is it incorrect to format the code with line-breaks at the ;? It would help me with the code. :) Commented Jan 17, 2012 at 2:38

2 Answers 2

2

You might try:

lblMsg.InnerHtml = @"
    <script type='text/javascript'>
        if(confirm('Do you want to continue?')) {
            window.location='TestPage.aspx?ID=" + Request.QueryString["ID"].ToString() + "&txtTest=" + Server.UrlEncode(txtTest.Text) + strSomeString + "'; 
        }
    </script>";

The confirm function returns a bool depending on whether or not the user confirmed the choice. You can use that to redirect to the next page only if necessary.

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the returned answer from confirm dialog. And depending on that do what you want. you need to edit your javascript like this,

var answer = confirm("Do you want to continue?")
   if (answer){
            window.location='TestPage.aspx?ID="' + Request.QueryString["ID"].ToString() +  "&txtTest=" + Server.UrlEncode(txtTest.Text) + strSomeString + "\""; 
    return false;
   }

2 Comments

This format is definitely much easier to understand. Although, from a C# point of view, the bool if-block semantics seem a bit odd. But that's OT ...I digress...
...or maybe it's the return false; that is bugging me. Why is that needed? Just asking because it seems out of place and useless ...???

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.