简体   繁体   English

jQuery UI确认对话框和asp.net回发

[英]jQuery UI confirmation dialog and asp.net postback

I have a delete button in a gridview. 我在gridview中有一个删除按钮。 For those not familiar with asp.net my delete button outputs like so: 对于那些不熟悉asp.net的人,我的删除按钮输出如下:

<a id="ctl00_cp1_dtgrAllRates_ctl02_lbDelete" 
   class="lb"
   href="javascript:__doPostBack('ctl00$cp1$dtgrAllRates$ctl02$lbDelete','')">
Delete</a>

I have a confirmation dialog hooked up to all the delete links in the gridview to ask the user if they are sure they want to delete. 我有一个确认对话框,连接到gridview中的所有删除链接,询问用户是否确定要删除。 It pops up no problem but I want to fire the postback (the href value) if they click confirm. 它弹出没问题,但我想点击回发(href值),如果他们点击确认。 I'm not sure how to do this as the dialog code is seperate to the link that is clicked so I can't just grab the href on 'this' eg 我不知道如何做到这一点,因为对话框代码与单击的链接分开,所以我不能只抓住'this'上的href,例如

var theID = $(this).attr("href");

and fire that. 然后解雇 Is there some way I can pass the href val as a parameter to the dialog code or something so that the 'Confirm Delete' section uses it when the button is clicked and if 'Cancel' is clicked the dialog just closes? 有没有什么方法可以将href val作为参数传递给对话框代码或某些东西,以便“确认删除”部分在单击按钮时使用它,如果单击“取消”,对话框就会关闭?

Here is my jQuery code: 这是我的jQuery代码:

$(document).ready(function(){
    $("#dialog").dialog({
      bgiframe: true,
      autoOpen: false,
      width: 400,
      height: 200,
      modal: true,
      buttons: {
                'Confirm Delete': function() {
                    $(this).dialog('close');
                    //fire href here preferably
                    },
                Cancel: function(){
                    $(this).dialog('close');
                    }
            }
    });

    $(".lb").click(function(event){
        $("#dialog").dialog('open');
        event.preventDefault();
    });

});

TIA TIA

Lloyd 劳埃德

Ok, managed to solve it. 好的,设法解决它。 I came across this post which helped a little: 我发现这篇文章有点帮助:

How to implement "confirmation" dialog in Jquery UI dialog? 如何在Jquery UI对话框中实现“确认”对话框?

However the example provided in the post wasn't quite working simply because the instantiation of the dialog was incorrect on the click handler. 但是,帖子中提供的示例并不是很简单,因为对话框的实例化在单击处理程序上是不正确的。 There is a different way of setting properties/options on the dialog once a dialog has already been instantiated. 一旦对话框已经实例化,在对话框上设置属性/选项有一种不同的方法。 So my final code was: 所以我的最终代码是:

$(document).ready(function(){

$("#dialog").dialog({
  modal: true,
        bgiframe: true,
        width: 500,
        height: 200,
  autoOpen: false
  });


$(".lb").click(function(e) {
    e.preventDefault();
    var theHREF = $(this).attr("href");


    $("#dialog").dialog('option', 'buttons', {
            "Confirm" : function() {
                window.location.href = theHREF;
                },
            "Cancel" : function() {
                $(this).dialog("close");
                }
            });

    $("#dialog").dialog("open");

});

});

Hope this helps someone else. 希望这有助于其他人。 Gurdas, thanks for your help, it definately got the gears turning. Gurdas,谢谢你的帮助,它绝对让齿轮转动。 :) :)

There's probably a cleaner way to do this, but i'd think you'd have to nab the context of the link you click in order to use its href in the construction of the dialog; 可能有一种更简洁的方法可以做到这一点,但我认为你必须抓住你点击的链接的上下文,以便在构建对话框时使用它的href; and then fire the dialog's open even after it's been constructed with that parameter; 然后,即使用该参数构造了对话框,也会激活该对话框; i'm going to think a little more about a more efficient method, but hopefully this gets a few gears turning... 我将更多地考虑一种更有效的方法,但希望这会让一些齿轮转向......

 $(".lb").click(function(event){    

      var theHREF = $(this).attr("href");



       $("#dialog").dialog({
      bgiframe: true,
      autoOpen: false,
      width: 400,
      height: 200,
      modal: true,
      buttons: {
                'Confirm Delete': function() {

                    //href fired here
                    window.location.href= theHREF; 

                    },
                Cancel: function(){
                    $(this).dialog('close');
                    }
            }    

    }).dialog('open');

Some times ago I've build up a web control to do that, in a standard, integrated way. 很久以前,我已经建立了一个Web控件 ,以标准的集成方式完成。 Take a look. 看一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM