当单击 DataList 控件中的 LinkButton 时,我试图让 jQuery-UI 确认对话框工作,但我尝试过的任何方法似乎都不起作用。DataList 是数据库条目的文件附件列表。
这是原始 .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 函数是一个简单的 Yes/No 对话框,用于确认删除附件。这是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 事件。