title: How to persist temporary values between postbacks before inserting to the database?
body: <h2>General Problem:</h2>
  
  <p>I'm trying to create something similar to this:</p>
  
  <p><img src="https://i.stack.imgur.com/6sg3W.png" alt="enter image description here"></p>
  
  <p>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.</p>
  
  <h2>Question:</h2>
  
  <p>What is the best way to persist the product selections between Postbacks?  </p>
  
  <p>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.</p>
  
  <h2>My current approach:</h2>
  
  <p>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.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The ProductListItem class:</p>
  
  <pre><code>&lt;Serializable()&gt;
  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
  </code></pre>
  
  <p>The Add Button Click handler:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks for your time!</p>