简体   繁体   中英

Conditional confirm box in c#

How to call a conditional confirm box from c#.

I have 2 hidden fields and based on the condition I want to call confirm box.

After that I also want what user has pressed (clicked on yes or No).

Design:-

  <input type="submit" id="btnAddPaymentMethod" onserverclick="AddPaymentMethod_Click" runat="server" value="add payment method" />

Code:-

   protected void Next_Click(object sender, EventArgs e)
     {
            if (hdnDefault.Value == hdnPrimary.Value) { return; }
            else
            {
            //open confirm box 
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Do you want to save new default payment method?');", true);
                    string confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
                    }
            }
     }

I have tried below jQuery Code:-

 function Confirm(msg) {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm(msg)) {
                confirm_value.value = "Yes";
                $('#btnAddPaymentMethod').click();
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
protected void Next_Click(object sender, EventArgs e)
{
  if (hdnDefault.Value == hdnPrimary.Value) { 
    return; 
  } else {
    //open confirm box 
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "Confirm('Do you want to save new default payment method?');", true);
  }
}

protected void AddPaymentMethod_Click(object sender, EventArgs e)
{
  string confirmValue = Request.Form["confirm_value"];
  if (confirmValue == "Yes") {
    ScriptManager.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
  } else {
    ScriptManager.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
  }
}

function Confirm(msg) {
  var confirm_value = document.createElement("INPUT");
  confirm_value.type = "hidden";
  confirm_value.name = "confirm_value";
  confirm_value.value = confirm(msg)? "Yes" : "No";
  document.forms[0].appendChild(confirm_value);
  $('#btnAddPaymentMethod').click();
}

I have not run your code. But when you have runat="server" for an input control it will append with the asp.net unique Id. So try to access the input control by it's name ending with btnAddPaymentMethod as below.

Change from $('#btnAddPaymentMethod').click();to $('[id$=btnAddPaymentMethod]').click();

This jQuery code will open a confirm dialog, with an 'ok' and 'cancel' buttons.

Here an anchor tag with an id of myConfirmPageLink, when clicked, will ask for confirmation. If ok is clicked it proceeds to the target, and if cancel is clicked it stays on the same page.

$("a#myConfirmPageLink").click(function(){
    return confirm("Are you sure you want to go to that page/site ?");
});

This should be easy to modify for your purposes.

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