How to popup a confirmation box on condition on ASPX page?

I'have seen several tutorial/demos but can't solve my problem for my ASPX page(C#). I want popup a confirmation message box with Yes & No buttons on a button click event but after checking a condition .

After clicking the button it will check the condition like, if(a>b) , if condition is true confirmation box will be poped up otherwise skip and no message will be poped up. After that If user click Yes button on confirmation message box, it will proceed further.

Please provide the code as I'm Beginner.


Below is a really simple example for a beginner using Bootstrap.Simple copy and paste the code below and it will work:

Code behind(.cs file):

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnClick_Click(object sender, EventArgs e)
{
    lblOutput.Text = String.Empty;
    bool showModal = true;

    if(showModal)
        ScriptManager.RegisterStartupScript(this, this.GetType(), "myModal", "$('#myModal').modal('show');", true);
}

protected void Decision_Command(object sender, CommandEventArgs e)
{
    lblOutput.Text = "User clicked - " + e.CommandArgument;
}

.ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="btnClick" runat="server" Text="OK" OnClick="btnClick_Click" />
        <asp:Label ID="lblOutput" runat="server"></asp:Label>
        <div id="myModal" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Would you like to continue?</h4>
                    </div>
                    <div class="modal-body">
                        <h3>Would you like to coninue?</h3>
                        <asp:Button ID="btnYes" runat="server" Text="Yes" OnCommand="Decision_Command" CommandArgument="Yes" />
                        <asp:Button ID="btnNo" runat="server" Text="No" OnCommand="Decision_Command" CommandArgument="No" />
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

Output:

使用Bootstrap弹出确认


This should get you on the right track. yes and no aren't implemented by confirm() You have to switch to an alternative like sweetalert

var select = document.getElementById('c'),
    btn    = document.getElementById('check');

// Add click event
btn.onclick = function(){

   var value = select.options[select.selectedIndex].value; // I could also use Boolean(value); to convert it to boolean and not use quote for comparsion.
 
   // Checking if condition is true. If yes, then ask for user confirmation.
   if(value == '1'){
     
      console.log('condition is true; asking for confirmation');
     
      var ask = confirm('The condition is true, do you want to continue?');
      // if confirmed;
      if(ask){
         console.log('confirmed');
      // if denied/cancelled
      } else {
         console.log('cancelled');
      }
   }

}
Condition:
<select id="c">
   <option value="1">true</option>
   <option value="0">false</option>
</select>
<button id="check">Check</button>
链接地址: http://www.djcxy.com/p/71546.html

上一篇: 使用来自Hibernate实体的现有数据填充envers修订表

下一篇: 如何在ASPX页面上弹出确认框