【问题标题】:Remove item from DataSource when DataList ItemCommand is fired触发 DataList ItemCommand 时从 DataSource 中删除项目
【发布时间】: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。

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

谢谢

【问题讨论】:

    标签: asp.net vb.net datalist


    【解决方案1】:

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

    <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 中。

    【讨论】:

    • 谢谢你——成功了。我真的很感谢你也抽出时间来演示这个。再次感谢。
    【解决方案2】:

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

    If e.CommandName = "Del" Then
    
        MyObjectList.Remove(myObject)
        dl.Datasource = MyObjectList
        dl.databind()
    ....
    End If
    

    【讨论】:

    • 这就是我如何删除用户单击删除按钮的对象的问题?由于 DataList 控件的 ItemCommand 事件下的 e.Item.DataItem 是 Nothing,所以投射它没有任何区别?
    • 在 MyObjectList 中如何填充数据,可以使用数据库(数据库中的表)作为数据源。
    • 我使用 List (Of SomeObject) 作为数据源,然后将其绑定到 DataList 的数据源。为了让我删除一个项目,我正在检查 ItemCommand 中的“e”变量事件,这没什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2015-06-24
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多