2014-10-07 70 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