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)
Visual Basic (Declaration)
Public Function Add ( _
key As String, _
value As Object _
) As StateItem
Dim instance As StateBag
Dim key As String
Dim value As Object
Dim returnValue As StateItem
returnValue = instance.Add(key, value)
public StateItem Add (
string key,
Object value
)
public:
StateItem^ Add (
String^ key,
Object^ value
)
public StateItem Add (
String key,
Object value
)
public function Add (
key : String,
value : Object
) : StateItem
Parameters
- key
The attribute name for the StateItem.
- value
The value of the item to add to the StateBag.
Return Value
Returns a StateItem that represents the object added to view state.
| Exception type | Condition |
|---|
ArgumentException | key is a null reference (Nothing in Visual Basic). - 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
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.");
}
}
function MovePiece(fromPosition : String, toPosition : String) {
var bag : StateBag = ViewState;
var 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.");
}
}
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0, 1.1, 1.0