具有以下带有复选框列表的中继器控件:
<asp:Repeater ID="rptItemList" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div>
<asp:CheckBox ID="chkItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ItemName").ToString() %>' />
<asp:HiddenField ID="hdItem" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ItemId").ToString() %>' />
</div>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbtnDel" runat="server" Text="Delete" OnClick="lbtnDel_Click" OnClientClick="return confirm('Are you sure you want to delete selected items from this list?')"></asp:LinkButton>
</FooterTemplate>
</asp:Repeater>
并遵循后面的代码来处理 lbtnDel_Click 事件:
protected void lbtnDel_Click(object sender, EventArgs e)
{
foreach (RepeaterItem ri in rptItemList.Items)
{
CheckBox chk = (CheckBox)ri.FindControl("chkItem");
HiddenField hd = (HiddenField)ri.FindControl("hdItem");
if (chk.Checked)
{
var tc = new ItemController();
tc.DeleteItem(Convert.ToInt32(hd.Value));
}
}
Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
}
当我选择一个复选框并单击删除时,代码会找到该复选框但将其读取为未选中,因此不会删除该项目。
有任何想法吗?