StateBag.Add Method (String, Object)
.NET Framework (current version)
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.
Assembly: System.Web (in System.Web.dll)
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.StateItemReturns 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.
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."); } }
.NET Framework
Available since 1.1
Available since 1.1
Show: