c#中的条件确认框

时间:2017-02-07 04:54:00

标签: c# jquery confirm

如何从c#调用条件确认框。

我有2个隐藏字段,并根据我要调用的条件确认框。

之后我还想要用户按下了什么(点击是或否)。

设计: -

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

代码: -

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

我在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);
        }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我没有运行你的代码。但是当你有runat="server"输入控件时,它会附加asp.net唯一ID。因此,尝试使用以btnAddPaymentMethod结尾的名称来访问输入控件,如下所示。

$('#btnAddPaymentMethod').click();更改为$('[id$=btnAddPaymentMethod]').click();

答案 2 :(得分:0)

这个jQuery代码将打开一个确认对话框,带有'ok'和'cancel'按钮。

这里有一个id为myConfirmPageLink的锚标签,点击后会要求确认。如果单击确定,则进入目标,如果单击取消则保留在同一页面上。

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

这应该很容易为您的目的修改。