2014-10-07 63 views
1

我有一些行的gridview。 在每一行上,我都在网格右侧有一个图像按钮,可以删除一条记录。在jQuery UI对话框中回传

点击imagebutton显示一个使用jQuery UI创建的对话框。

jQuery代码是:

$("[name*='btn_check']").click(function() { 
    event.preventDefault(); 
    $("#dialog-confirm").dialog({ 
     autoOpen: true, 
     resizable: false, 
     height: 200, 
     modal: true, 
     buttons: { 
      "Accept": function() { 
       $(this).dialog("close"); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 
}); 

代码是jQuery用户界面对话框非常简单和普遍。

所以,现在我想单击“接受”按钮时执行代码,我虽然__doPostBack可能是一个很好的解决方案。

所以,我在我的GridView创建了一个隐藏按钮(靠近ImageButton的),然后我把这个添加代码为“接受”功能,我在另一个StackOverflow的问题发现:

__doPostBack('btn_hidden', ''); 

我也试图用这个:

__doPostBack('<%= btn_hidden.UniqueID %>', ''); 

但没有成功。

那么,哪个是执行回发的正确方法,以及如何发送记录的ID以删除后面带有代码的记录?

回答

1

首先,您应该在您的ImageButton上设置正确的CommandNameCommandArgument。然后从OnClientClick调用对话框。我的理解只有一个对话元素隐藏的地方,所以应该用IDS没有问题:

<asp:ImageButton runat="server" 
    CommandName="Delete" 
    CommandArgument='<%# Eval("YourKeyFieldNameHere") %>' 
    OnCommand="ImageButton_Command" 
    OnClientClick="javascript:return showConfirmDialog(this.name)" 
/> 

function showConfirmDialog(uniqueId) { 
    $("#dialog-confirm").dialog({ 
     autoOpen: true, 
     resizable: false, 
     height: 200, 
     modal: true, 
     buttons: { 
      "Accept": function() { 
       $(this).dialog("close"); 
       __doPostBack(uniqueId, ''); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 

    return false; // this is to prevent default click handler to cause a postback 
} 

代码隐藏:

protected void ImageButton_Command(object sender, CommandEventArgs e) 
{ 
    // e.CommandArgument will contain the record key and 
    // e.CommandName will be equal to "Delete" or whatever you'll set on aspx 
} 
+0

您的解决方案运作良好!我在'function showConfirmDialog(uniqueId){'后面加了'event.preventDefault();'因为它返回“无效的回发或回调参数”错误。谢谢! – 2014-10-07 10:31:16

+0

@ s.milziadi实际上在对话调用之前移除'return'就足够了。然后它会返回false,并且不会有回传。我更新了代码。感谢您指出。 – 2014-10-07 12:33:06

+0

你的解决方案绝对是最好的,因为event.preventDefault();与IE和Chrome一起工作,但在Firefox中无法正常工作导致错误。再次感谢 – 2014-10-08 10:55:37