StateBag.Add(String, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public:
System::Web::UI::StateItem ^ Add(System::String ^ key, System::Object ^ value);
public System.Web.UI.StateItem Add (string key, object value);
member this.Add : string * obj -> System.Web.UI.StateItem
Public Function Add (key As String, value As Object) As StateItem
参数
返回
返回表示添加到视图状态的对象的 StateItem。
例外
示例
下面的代码示例演示如何使用 Add 方法。
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.");
}
}
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