StateBag.Add(String, Object) Method

Definition

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.

C#
public System.Web.UI.StateItem Add(string key, object value);

Parameters

key
String

The attribute name for the StateItem.

value
Object

The value of the item to add to the StateBag.

Returns

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

Exceptions

key is null.

-or-

The number of characters in key is 0.

Examples

The following code example demonstrates using the Add method.

C#
void MovePiece(string fromPosition, string toPosition) {
   StateBag bag = ViewState;
   object piece = bag[fromPosition];
   if (piece != null) {
      bag.Remove(fromPosition);
      bag.Add(toPosition, piece);
      RenderBoard();
   }
   else {
      throw new InvalidPositionException("There is no game piece at the \"from\" position."); 
   }
}

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also