简体   繁体   中英

How does user confirmation message box work in ASP.Net

How does the client know to send request to server only when the confirmation box result is ok and stay on page if cancelled? Also, is this mechanism any different in ASP.Net and ASP.Net MVC?

The responses I got seem to tell me how to implement the functionality. I want to know the internal working of when user clicks OK/cancel what happens internally. How does browser come to know it has to proceed to server call or close itself and do nothing?

You can use simple confirm box

$("#callConfirm").on("click", function(e) {
    if(confirm('Are you sure'))
        alert('Yes');
    else
        alert('No');
});

JSFIDDLE

I write this Jquery code in the aspx page for button.

Steps:

  1. Add a button for which you want to have a confirm box, add its jquery and div to show the confirm box.

  2. Register the script for button on Page_Load, and write the methods which will bind this script on button.

  3. Also do not forget the server side method or event for button click, which will be continued after OK confirmation from confirm box.

  4. If cancel is clicked, nothing will happen and div will be closed.

     <script type="text/javascript"> function FileItem(callBackFunction, title, content) { $("#File-confirm").html(content).dialog({ autoOpen: true, modal: true, title: title, resizable: false, height: 140, close: function (event, ui) { $(this).dialog("destroy"); }, buttons: { 'Ok': function () { callBackFunction(); $(this).dialog("destroy"); }, 'Cancel': function () { $(this).dialog("destroy"); } }); } } 

where SaveBtn is the button in the UI:

<asp:Button ID="SaveBtn" runat="server" Text="File"  OnClick="SaveBtn_Click"/>
<div id="File-confirm" style="display: none">
</div>

Again the code behind:

FileConfirmRequest(SaveBtn, "Confirm", "Are you sure you want to file the changes?");
// In the Page_Load, write the above code
//Use this method later on the page
protected void FileConfirmRequest(Button control, string title, string message)
{
  string postBackReference = Page.ClientScript.GetPostBackEventReference(control, String.Empty);
  string function = String.Format("javascript:FileItem(function() {{ {0} }}, '{1}', '{2}'); return false;", postBackReference, title, message);
  control.Attributes.Add("OnClick", function);
}

Now, The Onclick of the Button:

protected void SaveBtn_Click(object sender, EventArgs e)
 {
   //Do what you want to after OK Click from the confirm box
 }

Try This

<script>
   function CallConfirm()
    {
    if(confirm('Are you sure'))
      //do your stuff
    else
     return false;
    }

<script />

and on asp button write like this

<asp:Button id="Click" runat="server" onclientclick="return  CallConfirm();"  onclick="btn_Click"/>

you can use bootstrap in asp.net and it will help you give new look and help you in lots of other things,see this http://getbootstrap.com/ and u can use bootstrap confirmation box.

 <asp:button 
    id="Button1" runat="server" text="Button" xmlns:asp="#unknown">OnClientClick="return confirmation();" onclick="Button1_Click"/>
</asp:button>

        <script type="text/javascript">
        function confirmation() {
        if (confirm('are you sure you want to delete ?')) {
        return true;
        }else{
             return false;
                    }
                }
    </script> 

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