Monday, June 7, 2010

System.Web.UI.Pair

The Pair class is used as a basic structure to store two related objects. It is a utility class that is used in various ways throughout ASP.NET, such as during page state management tasks or in configuration section handlers. You can use the Pair class in your own code anywhere that you need a structure to contain two related objects.

Through following example I am going to illustrate it's use in control state.

Suupose you are overriding any ASP.NET control (for eg. GridView) and lets say view state is off. Now in this case we can use control state to store some custom properties.
Protected Overrides Function SaveControlState() As Object
'Get the state from base class

Dim baseState As Object = MyBase.SaveControlState()

'Combine this state with objects you want to store and return
'the final object.
Return New Pair(baseState, CurrentPageIndex) 'Where CurrentPageIndex is some custom property.
End Function


Protected Overrides Sub LoadControlState(ByVal savedState As Object)
Dim pair As Pair = savedState
If Not IsNothing(pair) Then
'Give the base class its state.
MyBase.LoadControlState(pair.First)
'Now process the state you saved.
CurrentPageIndex = pair.Second
End If
End Sub

No comments:

Post a Comment