JQuery的对话和ASP.NET直放站 [英] JQuery DIalog and ASP.NET Repeater
问题描述
我有一个ASP.NET中继器,显示与删除的LinkButton项的列表
I have an ASP.NET repeater that shows a list of items with a delete LinkButton.
我想设置的删除LinkButtons显示一个jQuery对话框进行确认。如果点击OK按钮,我想做回发。
I want to setup the Delete LinkButtons to show a JQuery Dialog for a confirmation. If the "OK" button is clicked, I want to do the postback.
最明显的问题是,每个LinkButton的在中继器将有它自己的ID,我不希望有复制所有的对话框中的JavaScript。
The obvious problem is that each LinkButton in the repeater will have it's own ID and I don't want to have to duplicate all the javascript for the dialog.
建议?
推荐答案
解决方法并非如此简单。您必须调用$ P $后,原来的回调函数pssing jQuery UI的对话框的确定按钮的能力。
The solution is not so simple. You must have the ability to call the original callback function after pressing the Ok button of jQuery UI Dialog.
首先你需要一个通用的js函数来显示对话框:
First you need a generalized js function for showing the dialog:
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的presence像
I supposed the presence of a div like
<div id="divConfirm"></div>
在C#code-背后,你必须注册previous客户端功能,通过你的控件作为参数的初始asp.net callbackFunction参数(我广义):
On c# code-behind you have to register the previous client function, passing the original asp.net callbackFunction of your control as parameter (I generalized):
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分配给控件回发功能的能力的方法。
Through the method GetPostBackEventReference you have the ability to retrieve the postback function that asp.net assign to the control.
现在,对直放站的ItemDataBound,检索执行删除的控制,并将它传递给这个函数:
Now, on Repeater ItemDataBound, retrieve the control that execute the delete and pass it to this function:
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound">
...
<ItemTemplate>
...
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
...
</ItemTemplate>
</asp:Repeater>
和code:
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???");
}
}
我希望这有助于。
I hope this helps.
这篇关于JQuery的对话和ASP.NET直放站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!