我有一个 ASP.NET 转发器,它显示带有删除 LinkButton 的项目列表。
我想设置 Delete LinkButtons 以显示 JQuery 对话框以进行确认。如果单击“确定”按钮,我想进行回发。
明显的问题是转发器中的每个 LinkButton 都有自己的 ID,我不想为对话框复制所有的 javascript。
建议?
最佳答案
解决方案并不那么简单。您必须能够在按下 jQuery UI 对话框的 Ok 按钮后调用原始回调函数。
首先你需要一个通用的js函数来显示对话框:
function showConfirmRequest(callBackFunction, title, content)
{
$("#divConfirm").html(content).dialog({
autoOpen: true,
modal: true,
title: title,
draggable: true,
resizable: false,
close: function(event, ui) { $(this).dialog("destroy"); },
buttons: {
'Ok': function() { callBackFunction(); },
'Cancel': function() {
$(this).dialog("destroy");
}
},
overlay: {
opacity: 0.45,
background: "black"
}
});
}
我想像这样的 div 的存在
<div id="divConfirm"></div>
在 c# 代码隐藏中,你必须注册以前的客户端函数,将你的控件的原始 asp.net callbackFunction 作为参数传递(我概括了):
protected void AddConfirmRequest(WebControl control, string title, string message)
{
string postBackReference = Page.ClientScript.GetPostBackEventReference(control, String.Empty);
string function = String.Format("javascript:showConfirmRequest(function() {{ {0} }}, '{1}', '{2}'); return false;",
postBackReference,
title,
message);
control.Attributes.Add("onclick", function);
}
通过 GetPostBackEventReference 方法,您可以检索 asp.net 分配给控件的回发函数。
现在,在 Repeater ItemDataBound 上,检索执行删除的控件并将其传递给此函数:
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound">
...
<ItemTemplate>
...
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
...
</ItemTemplate>
</asp:Repeater>
和代码:
protected void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
WebControl btnDelete = ((WebControl)e.Item.FindControl("btnDelete"));
AddConfirmRequest(btnDelete, "Confirm delete", "Are you sure? Really???");
}
}
希望对您有所帮助。
关于asp.net - JQuery DIalog 和 ASP.NET Repeater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1570329/