我試圖讓一個jQuery-UI確認對話框在點擊DataList控件中的LinkButton時工作,但沒有任何我嘗試過似乎工作。 DataList是數據庫條目的文件附件列表。在ASP.NET DataList控件中的jQuery-UI確認對話框
這裏是原來的.aspx文件的相關部分:
<ul id="attachmentList">
<asp:DataList ID="dlAttachments" runat="server" DataKeyField="AttachID" RepeatLayout="Flow">
<ItemTemplate>
<li>
<asp:HyperLink ID="hlViewAttach" runat="server" NavigateUrl='<%# CreateAttachURL(Convert.ToInt32(Eval("AttachID"))) %>'
Text='<%# Eval("Description").ToString() %>' Target="_blank" />
<em>(</em><asp:Label ID="lblType" runat="server" Text='<%# GetFriendlyFileType(Eval("FileType").ToString()) %>' Font-Italic="True"></asp:Label>,
<asp:Label ID="lblSize" runat="server" Text='<%# GetFriendlyFileSize(Convert.ToInt32(Eval("FileSize"))) %>' Font-Italic="True"></asp:Label><em>)</em>
<asp:LinkButton ID="btnDelAttach" runat="server" OnClick="btnDelAttach_Click"
OnClientClick="return ConfirmDeleteAttachment(this, 'Please confirm deletion', 'Are you sure you wish to delete this attachment?');">DELETE</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ul>
下面是上述由ASP.Net作爲生成的HTML:
<ul id="attachmentList">
<span id="ctl00_mainContent_wizNewIPR_dlAttachments">
<li><a id="ctl00_mainContent_wizNewIPR_dlAttachments_ctl00_hlViewAttach" href="ViewAttachment.aspx?AttachID=260" target="_blank">Attachment description #1</a>
<em>(</em><span id="ctl00_mainContent_wizNewIPR_dlAttachments_ctl00_lblType" style="font-style:italic;">Acrobat PDF document</span>,
<span id="ctl00_mainContent_wizNewIPR_dlAttachments_ctl00_lblSize" style="font-style:italic;">74.61 kB</span><em>)</em>
<a onclick="return ConfirmDeleteAttachment(this, 'Please confirm deletion', 'Are you sure you wish to delete this attachment?');"
id="ctl00_mainContent_wizNewIPR_dlAttachments_ctl00_btnDelAttach" href="javascript:__doPostBack('ctl00$mainContent$wizNewIPR$dlAttachments$ctl00$btnDelAttach','')">DELETE</a></li>
</span>
</ul>
我使用jQuery的功能一個簡單的是/否對話框來確認刪除附件。這裏的JavaScript源:
//Confirm Delete Attachment dialog
var delConfirmed = false;
function ConfirmDeleteDialog(obj, title, dialogText) {
if (!delConfirmed) {
//add the dialog div to the page
$('body').append(String.Format("<div id='confirmDeleteDialog' title='{0}'><p>{1}</p></div>", title, dialogText));
//create the dialog
$('#confirmDeleteDialog').dialog({
modal: true,
resizable: false,
draggable: false,
close: function(event, ui) { $('body').find('#confirmDeleteDialog').remove(); },
buttons:
{
'Yes, delete it': function() {
$(this).dialog('close');
delConfirmed = true;
if (obj) obj.click();
},
'No, keep it': function() {
$(this).dialog('close');
}
}
});
}
return delConfirmed;
}
我得到這個從http://markmintoff.com/2011/03/asp-net-jquery-confirm-dialog/ - 它看起來很簡單,是有道理的,但由於某種原因,它不是爲我工作。我從來沒有得到jQuery-UI確認對話框,它只是當我點擊按鈕時觸發我按鈕的OnClick事件。
我通過電子郵件發送了Mark Mintoff(發佈我使用的代碼的開發人員),他指出我的函數調用(「ConfirmDeleteAttachment」)與函數的名稱(「ConfirmDeleteDialog」)不同。多麼尷尬!我不知道有多少個小時我被這樣一個愚蠢的錯誤浪費了! – timbck2