AJAX.NET订阅重新排序列表ItemCommand或者DeleteCommand?
时间:2020-03-06 14:53:49 来源:igfitidea点击:
我想订阅我页面上的Reorderlist的ItemCommand事件。前端看起来像这样...
<cc1:ReorderList id="ReorderList1" runat="server" CssClass="Sortables" Width="400" OnItemReorder="ReorderList1_ItemReorder" OnItemCommand="ReorderList1_ItemCommand"> ... <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="delete.jpg" CommandName="delete" CssClass="playClip" /> ... </cc1:ReorderList>
在后端,我在Page_Load上有这个
ReorderList1.ItemCommand += new EventHandler<AjaxControlToolkit.ReorderListCommandEventArgs>(ReorderList1_ItemCommand);
并定义了此功能
protected void ReorderList1_ItemCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.CommandName == "delete")
{
//do something here that deletes the list item
}
}
}
尽管我尽了最大的努力,但我似乎无法使该事件开始。我们如何在ReorderList控件中正确订阅此事件?
解决方案
由于ImageButton的CommandName =" delete",我们应该连接到DeleteCommand事件而不是ItemCommand。
这有效:
<cc2:ReorderList ID="rlEvents" runat="server" AllowReorder="True" CssClass="reorderList"
DataKeyField="EventId" DataSourceID="odsEvents" PostBackOnReorder="False"
SortOrderField="EventOrder" OnDeleteCommand="rlEvents_DeleteCommand">
...
<asp:ImageButton ID="btnDeleteEvent" runat="server" CommandName="Delete" CommandArgument='<%# Eval("EventId") %>' ImageUrl="~/images/delete.gif" />
...
</cc2:ReorderList>
后面的代码:
protected void rlEvents_DeleteCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
// delete the item
// this will give you the DataKeyField for the current record -> int.Parse(e.CommandArgument.ToString());
//rebind the ReorderList
}

