【发布时间】:2013-04-24 15:32:03
【问题描述】:
我有点卡在这里。我有一个数据列表,它的数据源是 Lit (of SomeObject)。
Private MyObjectList As List (of SomeObject)
当用户点击按钮将产品添加到他们的购物篮并绑定数据列表时,我会加载数据。
Form_load....
dl.Datasource = MyObjectList
dl.databind()
End Sub
要在数据列表中显示数据,我有一个标签,显示他们添加的内容,使用 e eventArg 将其投射到我的对象,示例如下:
Protected Sub dl_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dl.ItemDataBound
Dim myObject = DirectCast(e.Item.DataItem, SomeObject)
....
Label.Text = myObject.Description
End Sub
内联代码是:
<asp:DataList ID="dl" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="DeletePRoduct" runat="server" Text="Delete" CommandName="Del" CommandArgument='<%# Container.ItemIndex %>'/>
</ItemTemplate>
</asp:DataList>
所以现在我希望用户能够删除产品
Protected Sub dl_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dl.ItemCommand
Dim myObject = DirectCast(e.Item.DataItem, SomeObject)
If e.CommandName = "Del" Then
MyObjectList.Remove(myObject)
....
End If
End Sub
如果我尝试在 ItemCommand 下传入 myObject,我意识到我投射的 e.item.DataItem 是 Nothing,因此它不会删除任何产品。然后,当我意识到我不能这样做时,我想按行索引删除该产品并在按钮上添加一个 CommandArgument,因为 List 需要一种 SomeObject。
谁能建议如何以这种方式删除对象?
谢谢
【问题讨论】: