如何在listview上执行不受控件约束的删除

时间:2011-12-09 16:48:24

标签: asp.net vb.net listview

我有一个ListView,我已经设置了删除链接按钮。当我点击删除但是我得到"The ListView 'ListView' raised event ItemDeleting which wasn't handled."所以我决定尝试实施DeleteLinkButton_Click()ListView_ItemDeleted() ...但我无法弄清楚如何识别我选择了哪一行我的DeleteLinkButton_Click()

我没有通过控件绑定我的源,而是使用了以下方法。

ListView.DataSource = myObject.RetreiveInfo()
ListView.DataBind()

我想如果我可以识别我的行,我可以访问那里的标签值并将它们传递给存储过程并执行我的删除。

有人可以帮忙吗?如果我需要提供更多代码,请告诉我!

编辑:

代码隐藏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   If Not IsPostBack Then
      UpdateDisplay()
   End If
End Sub

Protected Sub UpdateDistplay()
    ListView.DataSource = myObject.RetrieveInfo()
    ListView.DataBind()
End Sub

ASPX页面

<ItemTemplate>
  <tr>
      <td>
         <asp:LinkButton ID="DeleteLinkButton" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Delete this Info?')" runat="server"></asp:LinkButton>
      </td>
      <td>
         <asp:Label ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
      </td>
  </tr>
</ItemTemplate>

2 个答案:

答案 0 :(得分:3)

在页面设计中将此删除按钮放入。

<asp:LinkButton runat="server" ID="lbtnDelete" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.ID") %>'
                                OnClick="lbtnDelete_Click" CssClass="deleteButton" OnClientClick="return confirm('Are you sure?');">
                                    <img src="resources/images/icons/cross.png" alt="Delete" /></asp:LinkButton>

在页面后面写下此代码

protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        Tbl_Mode Tbl_Mode = DataClassesDataContext.Tbl_Modes.Single(p => p.ID == ID);
        DataClassesDataContext.Tbl_Modes.DeleteOnSubmit(Tbl_Mode);
        DataClassesDataContext.SubmitChanges();
        divDelete.Visible = true;
        Bind();
    }

答案 1 :(得分:2)

您可以在ItemDeleting事件中找到要删除的项目的索引,因此首先处理该项目可能会对您有所帮助。它看起来像这样:

Protected Sub myListView_OnItemDeleting(ByVal sender As Object, ByVal e As ListViewDeleteEventArgs)
    Dim deletedIndex As Integer
    deletedIndex = e.ItemIndex
End Sub

此时,您现在拥有单击了删除按钮的项目的索引,因此您可以查找标签并执行DELETE(正如您在问题中提到的那样)。

祝你好运!