笔记:asp.net自定义控件的视图状态管理
写了一段时间的asp.net代码后,对于asp.net自定义控件多了一份了解!重新看回以前有些模糊的自定义视图状态管理
在一个控件的复杂自定义属性中,此属性的状态可以由两种方法进行保存,但比较好的状态保存方法还是实现自定义视图管理,让其实现IStateManager接口
例如:
MyAddress控件MyControl的一个方法,存储省份,城市,地区三个字符串值
第一种方法是
声明对象可序列化,然后在控件里重载视图管理方法LoadViewState,SaveViewState
在其中添加状态管理状态:
Protected Overrides Function SaveViewState() As Object
Dim o1 As Object = MyBase.SaveViewState()
Dim p As New Pair(o1, Me.Address)
Return p
End FunctionProtected Overrides Sub LoadViewState(ByVal savedState As Object)
Try
Dim p As Pair = CType(savedState, Pair)
MyBase.LoadViewState(p.First)
Me._address = CType(p.Second, MyAddress)
Catch ex As Exception
MyBase.LoadViewState(savedState)
End Try
End Sub
此方法也能维持视图状态,但结果是因为完整保存了该属性完全的序列化结果,而导致页面视图大小急剧膨胀
第二个方法是对MyAddress实现视图接口IStateManager
<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class MyAddress
Implements IStateManager并实现其视图管理方法
#Region "视图状态"
Private _viewstate As StateBagProtected Property ViewState() As StateBag
Get
If _viewstate Is Nothing Then
_viewstate = New StateBag
If Me.IsTrackingViewState Then
CType(_viewstate, IStateManager).TrackViewState()
End If
End If
Return _viewstate
End Get
Set(ByVal value As StateBag)
_viewstate = value
End Set
End PropertyPrivate _IsTrackingViewState As Boolean = False
Protected ReadOnly Property IsTrackingViewState() As Boolean Implements System.Web.UI.IStateManager.IsTrackingViewState
Get
Return _IsTrackingViewState
End Get
End PropertyPublic Sub LoadViewState(ByVal state As Object) Implements System.Web.UI.IStateManager.LoadViewState
If state IsNot Nothing Then
CType(_viewstate, IStateManager).LoadViewState(state)
End If
End SubPublic Function SaveViewState() As Object Implements System.Web.UI.IStateManager.SaveViewState
Dim _savestate As Object = Nothing
If _viewstate IsNot Nothing Then
_savestate = CType(_viewstate, IStateManager).SaveViewState()
End If
Return _savestate
End FunctionPublic Sub TrackViewState() Implements System.Web.UI.IStateManager.TrackViewState
_IsTrackingViewState = True
If _viewstate IsNot Nothing Then
CType(_viewstate, IStateManager).TrackViewState()
End If
End SubProtected Sub SetDirty()
_viewstate.SetDirty(True)
End Sub
#End Region然后在MyControl中添加Address属性状态的跟踪与维持代码
Private _address As MyAddress
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
<PersistenceMode(PersistenceMode.InnerProperty)> _
<Browsable(True)> _
Public ReadOnly Property Address() As MyAddress
Get
If _address Is Nothing Then
_address = New MyAddress
If Me.IsTrackingViewState Then
CType(_address, IStateManager).TrackViewState()
End If
End IfReturn _address
End Get
End Property
#Region "视图状态管理"
Protected Overrides Sub TrackViewState()
If Me.Address IsNot Nothing Then
CType(Me.Address, IStateManager).TrackViewState()
End If
MyBase.TrackViewState()
End SubProtected Overrides Sub LoadViewState(ByVal savedState As Object)
Dim p As Pair = TryCast(savedState, Pair)
If p IsNot Nothing Then
MyBase.LoadViewState(p.First)
CType(Me.Address, IStateManager).LoadViewState(p.Second)
Return
End If
MyBase.LoadViewState(savedState)
End SubProtected Overrides Function SaveViewState() As Object
Dim baseobj As Object = MyBase.SaveViewState
Dim addobj As Object = Nothing
If Me.Address IsNot Nothing Then
addobj = CType(Me.Address, IStateManager).SaveViewState()
End If
If addobj IsNot Nothing Then
Return New Pair(baseobj, addobj)
Else
Return MyBase.SaveViewState()
End IfEnd Function
#End Region