Search code examples
asp.netvb.netdatalist

Remove item from DataSource when DataList ItemCommand is fired


Im a little stuck here. I have a datalist which has a datasource thats a Lit (of SomeObject).

Private MyObjectList As List (of SomeObject)

I load data as and when required by the user clicking a button to add a product to their shopping basket and bind the datalist.

Form_load....
dl.Datasource = MyObjectList
dl.databind()
End Sub

To display data in the datalist i have a label which shows what theyve added, using the e eventArg casting it to my object, example below:

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

The inline code is:

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

So now i would like the user to be able to delete a product

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

If i try and pass in myObject under the ItemCommand i realised that e.item.DataItem that i casted is Nothing so it doesnt delete any products. I then thought to remove that product by row index and added a CommandArgument to the button when i realised i cant do that as the List expects a type of SomeObject.

Could anyone advise how to remove an object in this manner?

Thanks


Solution

  • You can find ID of deleting item using dl.DataKeys[e.Item.ItemIndex]. Make sure to set DataKeyField of DataList control.

    enter image description here

    <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
    

    Note: you need to take care of the persistence of data after deleting. For example, storing data in ViewState, Session or Database.