Export (0) Print
Expand All

Pair Class

Holds two objects that can be added together to an ASP.NET server control's view state.

For a list of all members of this type, see Pair Members.

System.Object
   System.Web.UI.Pair

[Visual Basic]
Public Class Pair
[C#]
public class Pair
[C++]
public __gc class Pair
[JScript]
public class Pair

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can add this object to a server control's view state in the same way that you add a single object to view state. This class offers a more efficient way of adding groups of objects to a server control's view state.

Example

[Visual Basic] The following example creates a method that adds two objects to view state using the Pair class. It then creates a method to retrieve them and displays the values to a page.

[Visual Basic] 
' When a button is clicked, this method creates an instance of
' the Pair class, named myPair, adds it to the page's ViewState property,
' and displays user selections in a label.
  Sub btnAddToViewState_Click(sender As [Object], e As EventArgs)
    ' Create an instance and set it equal to the pair1 key from view state.
    Dim myPair As New Pair(pairTextBox.Text, pairList.SelectedItem.Text)
    myPair = ViewState("pair1")
    prLabel1.Text = "You added " & pairTextBox.Text & " and " & _
    pairList.SelectedItem.Text & " to view state."
End Sub 'btnAddToViewState_Click

' When a button is clicked, this method retrieves values from
' view state that were stored in an instance of the Pair class.
Sub btnRetrieve_Click(sender As [Object], e As EventArgs)

    ' Create variables to store the retrieved strings.
    Dim firstValue As String
    Dim secondValue As String

    ' Create an object to represent the view stae object,
    ' check to see that it exists, and if it does, 
    ' create a Pair object, myPair1, and set it to
    ' the object converted to a Pair instance. 
    Dim o As Object = ViewState("pair1")
    
    If o Is Nothing Then
      prLabel2.Text = "There is no object with the pair1 key in view state."
    Else
      Dim myPair1 As Pair = CType(o, Pair)

    ' Set the string variables to the Pair.First and
    ' Pair.Second fields, and display the retrieved text.
      firstValue = myPair1.First.ToString()
      secondValue = myPair1.Second.ToString()
      prLabel2.Text = "You retrieved " & firstValue & _
        secondValue & " from view state."
   End If
End Sub 'btnRetrieve_Click

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

Requirements

Namespace: System.Web.UI

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

Assembly: System.Web (in System.Web.dll)

See Also

Pair Members | System.Web.UI Namespace | ViewState | Maintaining State in a Control

Show:
© 2014 Microsoft