简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM