jQuery UI确认对话框和ASP.NET回发

3

我有一个在gridview中的删除按钮。对于不熟悉asp.net的人来说,我的删除按钮输出如下:

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

我已经在GridView中的所有删除链接上挂钩了一个确认对话框,以询问用户是否确定要删除。它弹出没有问题,但是如果他们点击确认,我希望触发postback(href值)。由于对话框代码与被点击的链接不同,因此我不知道该如何做到这一点,例如不能仅仅通过“this”来获取href。

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

并且删除它。是否有一种方法可以将href值作为参数传递给对话框代码或其他内容,以便在单击按钮时,“确认删除”部分使用它,并且如果单击“取消”,则对话框只是关闭?

这是我的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

Lloyd

3个回答

6

好的,问题已经解决了。我看到了这篇帖子,对我有些帮助:

如何在Jquery UI对话框中实现“确认”对话框?

然而,该帖子提供的示例并不完全有效,因为单击处理程序上的对话框实例化是不正确的。一旦对话框已经实例化,就有一种不同的方法来设置对话框的属性/选项。所以我的最终代码是:

$(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");

});

});

希望这能帮到其他人。Gurdas,感谢你的帮助,它确实让我思路清晰了。


3

可能有更简洁的方法,但我认为您需要捕获所点击链接的上下文,以便在对话框的构建中使用其href;然后在构建带有该参数的对话框后再打开它;我将思考更有效的方法,但希望这能启发一些思路...

 $(".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');

嗨Gurdas,感谢您的回复。我已经将您的代码放入其中,但它根本不起作用。当页面加载时,div会显示,而对话框甚至不会触发。希望您能再考虑一下。我现在正在苦思冥想。 :) - lloydphillips

0

前段时间我已经建立了一个Web控件,以标准、集成的方式来完成这个任务。请看一下。


请不要试图通过这个社区销售产品。如果可能的话,请尝试回答问题。 - Ravimallya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接