Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1945
  • Last Modified:

javascript confirm in c# code behind

Trying to figure out how to determine if the user hit 'OK' or 'Cancel' from the javascript confirm in code behind.

If the user hits 'OK' it needs to call another method else just return to the page.
ClientScript.RegisterStartupScript(this.GetType(), "Orders", "<script>confirm(" + strConfirmMessage + ")</script>");
        return;

Open in new window

0
chris_desborough
Asked:
chris_desborough
1 Solution
 
informaniacCommented:
Try using the vbscript MessageBox. It will give u a yes / no button which u could track.
0
 
Saqib KhanSenior DeveloperCommented:
ClientScript.RegisterStartupScript(this.GetType(), "Orders", "<script>msg=confirm(" + strConfirmMessage + ");if(msg){alert('ok was clicked')}</script>");
0
 
Anurag ThakurCommented:
as far as i understand the postback will happen only if the user select ok in the confirm box
if the user clicks cancel then nothing will happen and the control will remian on the page itself
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DeathraceCommented:
May be this helps
use the two events , onClientclick and onClick, onClick event will be triggered once the onClientclick event returns true.
So use a javascript funtion which pops a message box on client side and if that funtion returns true it will trigger the onclick event.
0
 
Rahul Goel ITILSenior Consultant - DeloitteCommented:
ClientScript.RegisterStartupScript(this.GetType(), "Orders", "<script>msg=confirm(" + strConfirmMessage + ");if(msg){ __dopostback(this, "okParameter");}else{__dopostback(this, "CancelParameter");}(</script>"); return;
0
 
chris_desboroughAuthor Commented:
Hi Rahu,

I had to use single quotes instead on okParameter and CancelParameter otherwise works ok. When the javascript is executed I get an error "Object expected". Debug says that __dopostback is not defined. Method in code snippet.
protected void LinkButton1_Click(object sender, EventArgs e)
    {
 
        strConfirmMessage = @"ORDER SUMMARY: Please review the items in your order\n\n";
 
        if (dt.Rows.Count > 0)
        {
 
            foreach (DataRow row in dt.Rows)
            {
 
                strConfirmMessage = @strConfirmMessage + (string)row["Desc"] + @"\n\n";
 
            }
 
 
            strConfirmMessage = @strConfirmMessage +
        @"\nClick OK to place your order or Cancel to continue modify your order.".ToString();
 
        ClientScript.RegisterStartupScript(this.GetType(), "Orders", "<script>msg=confirm('" + strConfirmMessage + "');if(msg){ __dopostback(this, 'okParameter');}else{__dopostback(this, 'CancelParameter');}</script>"); return; 
        
        }
 
 
        else
        {
            Response.Write("<script>alert('Unable to checkout - there are no items in the order.')</script>");
            return;
 
        }

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now