Export (0) Print
Expand All

StateBag.Add Method

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

[Visual Basic]
Public Function Add( _
   ByVal key As String, _
   ByVal value As Object _
) As StateItem
[C#]
public StateItem Add(
 string key,
 object value
);
[C++]
public: StateItem* Add(
 String* key,
 Object* value
);
[JScript]
public function Add(
   key : String,
 value : Object
) : StateItem;

Parameters

key
The attribute name for the StateItem object.
value
The value of the item to add to the StateBag.

Return Value

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

Exceptions

Exception Type Condition
ArgumentException Thrown when key is null or when the number of characters in key is 0.

Example

[Visual Basic, C#, JScript] The following example demonstrates using the Add method.

[Visual Basic] 
Sub GotoButton_Click(sender As Object, e As EventArgs)
   Dim invoice As InvoiceRecord = GetInvoice(GotoId.Text)
   GotoId.Text = ""
   ' Use the invoice Id as the key for the invoice
   ' name in the StateBag.
   ViewState.Add(invoice.Id, invoice.Name)
   DisplayInvoice(invoice)
   selectedMruValue = invoice.Id
End Sub 'GotoButton_Click

[C#] 
void GotoButton_Click(Object sender, EventArgs e) {
   InvoiceRecord invoice = GetInvoice(GotoId.Text);
   GotoId.Text = "";
   // Use the invoice Id as the key for the invoice
   // name in the StateBag.
   ViewState.Add(invoice.Id, invoice.Name);
   DisplayInvoice(invoice);
   selectedMruValue = invoice.Id;
}

[JScript] 
function GotoButton_Click(sender, e : EventArgs) {
   var invoice : InvoiceRecord = GetInvoice(GotoId.Text);
   GotoId.Text = "";
   // Use the invoice Id as the key for the invoice
   // name in the StateBag.
   ViewState.Add(invoice.Id, invoice.Name);
   DisplayInvoice(invoice);
   selectedMruValue = invoice.Id;
}

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

StateBag Class | StateBag Members | System.Web.UI Namespace | Introduction to Web Forms State Management | Item | Clear | Remove

Show:
© 2014 Microsoft