vml19
vml19

Reputation: 3864

JavaScript confirm from C# codebehind

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>";

Upvotes: 0

Views: 838

Answers (2)

Mike Christensen
Mike Christensen

Reputation: 91618

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.

Upvotes: 2

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

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;
   }

Upvotes: 0

Related Questions