1

我有点卡在这里。我有一个数据列表,它有一个 Lit(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。

谁能建议如何以这种方式删除对象?

谢谢

4

2 回答 2

1

您可以使用 找到删除项目的 ID dl.DataKeys[e.Item.ItemIndex]。确保设置DataKeyField控制DataList

在此处输入图像描述

<asp:DataList ID="dl" runat="server" DataKeyField="ID"...>
 ....
</asp:DataList>

Public Class SomeObject
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
End Class

Private _someObjects As List(Of SomeObject)
Public Property SomeObjects() As List(Of SomeObject)
    Get
        If _someObjects Is Nothing Then
            _someObjects = New List(Of SomeObject)() With { _
                New SomeObject() With { _
                    .ID = 1, _
                    .Name = "One" _
                }, _
                New SomeObject() With { _
                    .ID = 2, _
                    .Name = "Two" _
                }, _
                New SomeObject() With { _
                    .ID = 2, _
                    .Name = "Three" _
                } _
            }
        End If
        Return _someObjects
    End Get
    Set
        SomeObjects = value
    End Set
End Property

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    If Not IsPostBack Then
        dl.DataSource = SomeObjects
        dl.DataBind()
    End If
End Sub

Protected Sub dl_ItemCommand(source As Object, e As DataListCommandEventArgs)
    If e.CommandName = "Del" Then
        Dim id = Convert.ToInt32(dl.DataKeys(e.Item.ItemIndex))
        Dim someObject = SomeObjects.First(Function(x) x.ID = id)
        SomeObjects.Remove(someObject)

        dl.DataSource = SomeObjects
        dl.DataBind()
    End If
End Sub

注意:您需要注意删除后数据的持久性。例如,将数据存储在 ViewState、Session 或 Database 中。

于 2013-04-24T16:24:52.450 回答
1

DataList从数据源中删除对象后,您必须再次绑定MyObjectList才能在页面上获取更新的记录。

If e.CommandName = "Del" Then

    MyObjectList.Remove(myObject)
    dl.Datasource = MyObjectList
    dl.databind()
....
End If
于 2013-04-24T15:34:24.433 回答