简体   繁体   中英

Modal Confirm Box Using JQuery in ASP.Net

I want a similar behavior of “confirm delete” option in ASP.Net Gridview, as this questions shows How to add a "confirm delete" option in ASP.Net Gridview? but using Jquery Confirm Box.

I'm having a lot of problem with postback behavior and asp.net page flow.

I want domething simple as:

<asp:Button ID="ButtonRemove" runat="server" Text="<%$ Resources:Localizacao, BUTTON_REMOVE %>" OnClick="ButtonRemove_Click" OnClientClick="displayConfirmDialog();/>

Some idea how I can fire the OnClick event in javascript, or how a can put a Panel as confirm dialog?

*today I use ajaxcontroltoolkit, but as this was discontinued I'm trying to figure out some more elegant alternative.

1 - have a hidden div that is going to be your dialog box. Put this at the bottom of your HTML, outside your main page markup.

<div id="dialog1" class="dialog" style="display:none">
   SOME TEXT
   <a href="javascript://" onclick="confirmDialog()">CONFIRM</a>
</div>

Style it like so:

.dialog {
   position:absolute;
   width:250px;
   height:250px;
   background-color:#ffffff
}

.dialog a {
    display:block;
    padding:5px;
    margin-top:50px;
    background-color:#c0c0c0
}

2 - Center the dialog before showing it:

centerMe('#dialog1')

function centerMe(element) {
    //pass element name to be centered on screen
    var pWidth = jQuery(window).width();
    var pTop = jQuery(window).scrollTop()
    var eWidth = jQuery(element).width()
    var height = jQuery(element).height()
    jQuery(element).css('top', pTop + 100 + 'px')
    jQuery(element).css('left', parseInt((pWidth / 2) - (eWidth / 2)) + 'px')
}

3 - show the dialog:

jQuery('#dialog1').fadeIn('slow')

4 - create a function to handle the CONFIRM button:

function confirmDialog() {
    jQuery('#dialog1').hide()
    ... your code ...
}

If you want to get fancy you can create a background image for the dialog instead of a simple white background.

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