我在更新面板中使用listview。当我尝试从datatable删除其删除记录但listview没有更新。在listview中记录我的按钮代码:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ListView ID="lvSelectedVoucher" OnItemCommand="lvSelectedVoucher_OnItemCommand" OnLayoutCreated="lvSelectedVoucher_LayoutCreated" runat="server">
<LayoutTemplate>
<ItemTemplate>
<tr class="odd">
<td class="text-left" colspan="2">
<div id="mvRemoveSelectedId1" class="mv-action" title="Remove item" onclick="removeVoucher">
<asp:ImageButton class="ui-icon ui-icon-close" ID="imgDelete" OnClientClick="return confirm('Are you sure you want to delete this Voucher?')"
runat="server" CommandArgument='<%# Container.DataItemIndex +1%>' CommandName="Delete" />
</div>
<asp:Literal ID="ltlMerchant" Text='<%# Eval("MerchantName") %>' runat="server" ></asp:Literal>
</td>
我的服务器端代码如下:
protected void lvSelectedVoucher_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
int listcount = lvSelectedVoucher.Items.Count;
if (listcount - 1 == index)
{
DataTable curTable = (DataTable)ViewState["SelectedVoucher"];
curTable.Rows[index].Delete();
ViewState.Add("SelectedVoucher", curTable);
lvSelectedVoucher.DataSource = null;
lvSelectedVoucher.DataSource = curTable;
lvSelectedVoucher.DataBind();
UpdatePanel1.Update();
}
}
}
删除一条记录后如何刷新listview?提前致谢。
答案 0 :(得分:0)
这里的某些内容在逻辑上是不正确的。
您是否总是删除ListView的最后一项?您要将要删除的项目的索引与实际上在两行中的最后一项进行比较:
int listcount = lvSelectedVoucher.Items.Count;
if (listcount - 1 == index)
其他一切似乎都很好,那么,您是否尝试删除列表的最后一项视图并查看它是否刷新?
答案 1 :(得分:0)
我使用了我的内部ItemDeleting事件&amp; ListView正确刷新
protected void lvSelectedVoucher_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
int listcount = lvSelectedVoucher.Items.Count;
if (listcount == index+1)
{
DataTable curTable = (DataTable)ViewState["SelectedVoucher"];
curTable.Rows[index].Delete();
ViewState.Add("SelectedVoucher", curTable);
lvSelectedVoucher.DataSource = null;
lvSelectedVoucher.DataSource = curTable;
lvSelectedVoucher.DataBind();
UpdatePanel1.Update();
}
}