StateBag.Add Method (String, Object)

 

Adds a new StateItem object to the StateBag object. If the item already exists in the StateBag object, this method updates the value of the item.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Function Add (
	key As String,
	value As Object
) As StateItem

Parameters

key
Type: System.String

The attribute name for the StateItem.

value
Type: System.Object

The value of the item to add to the StateBag.

Return Value

Type: System.Web.UI.StateItem

Returns a StateItem that represents the object added to view state.

Exception Condition
ArgumentException

key is null.

- or -

The number of characters in key is 0.

The following code example demonstrates using the Add method.

Sub MovePiece(fromPosition As String, toPosition As String)
   Dim bag As StateBag = ViewState
   Dim piece As Object = bag(fromPosition)
   If Not (piece Is Nothing) Then
      bag.Remove(fromPosition)
      bag.Add(toPosition, piece)
      RenderBoard()
   Else
      Throw New InvalidPositionException("There is no game piece at the ""from"" position.")
   End If
End Sub 'MovePiece

.NET Framework
Available since 1.1
Return to top
Show: