General Problem:
I'm trying to create something similar to this:
The user can select a product from the dropdown, click add, and the result is another product row being added underneath in the "Product Added" section. A "Product Descriptor" must be selected for each product that has been added via the dropdown in each row. Only when the user clicks the "Submit" button does each of the product rows get inserted to the database.
Question:
What is the best way to persist the product selections between Postbacks?
I'm not worried about persisting the "Product Descriptor" selections, I believe I can do that. My main concern is finding the best way of storing these temporary selections before they are saved to the database.
My current approach:
The way I'm doing it now is to manage list of ProductListItem objects in the viewstate. This list is bound to a ListView that displays the added products.
Private Property SelectedProductList As List(Of ProductListItem)
Get
Return CType(ViewState("SelectedProductList"), List(Of ProductListItem ))
End Get
Set(ByVal value As List(Of ProductListItem ))
ViewState("SelectedProductList") = value
End Set
End Property
The ProductListItem class:
<Serializable()>
Public Class ProductListItem
Public Property ProductID As Integer
Public Property ProductName As String
' ProductDescriptor class represents Product Descriptor (details omitted)
Public Property Descriptor As ProductDescriptor
' Constructor omitted
End Class
The Add Button Click handler:
Protected Sub btnAddProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddProduct.Click
SelectedProductList.Add( New ProductListItem(...) )
ProductListView.DataSource = SelectedProductList
ProductListView.DataBind()
End Sub
I'm thinking to use the Session instead of the Viewstate here, because I have two other similar situations on the same page, where I'm keeping lists of objects in the viewstate, and I'm worried about the Viewstate getting corrupted or getting too big. What do you think?
Thanks for your time!