我目前有一个ListView,我正在填充一个绑定的Collection,我正在从一个预先存在的数据库填充。
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="false" /></td>
<td><%#DataBinder.Eval(Container.DataItem, "team_name") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "team_city") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "team_stateprov") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "team_country") %></td>
</tr>
</ItemTemplate>
基本上我要做的是这样的:当我点击我的删除按钮时,我想找到每个被检查的chkDelete,并在代码隐藏中获得相应的“id”的Container.DataItem,这样我就可以处理删除了从那里。可能吗?如果是这样,我应该怎么做呢?
答案 0 :(得分:0)
您只需要遍历ListView.Items
collection即可完成此任务。
在你的删除按钮点击事件中,你只需要这样的东西:
// Create some collection to store the IDs
List<string> itemsToDelete = new List<string>();
// Loop through all of the items, check if the box
// is checked, add to list if it is
foreach (ListViewItem item in yourListView.Items)
{
CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
if(chkDelete.Checked)
{
string yourID = item.DataItem["id"].ToString();
itemsToDelete.Add(yourID);
}
}
注意:我认为您可以按照我上面描述的方式访问DataItem字段。如果这不起作用,那么你必须将它转换为DataRowView并以这种方式抓住它:
DataRowView data = (DataRowView)item.DataItem;
string yourID = data["id"].ToString();
我使用这个控件已经有一段时间了,所以我只是不积极。但其中一个应该可以解决问题。