In ASP. NET, the properties of controls are closely related to viewstate. Only through viewstate can we identify and maintain the property values of controls. This article describes the relationship between attributes and viewstate, and describes how to access viewstate by attributes.
I. attributes and Viewstate
When you add "ASP. NET Server Control, the default type contains an example of text attribute writing, as shown in the following figure. Read and Write properties directly access viewstate, which is a common control attribute writing method. However, the writing of this property is inefficient, because the viewstate member is the object type, and every time the attribute is read, the members who retrieve the specified key value from viewstate are converted to the attribute type, the write property action is also directly written into viewstate.
Property Text() As String
Get
Dim s As String = CStr(ViewState("Text"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get
Set(ByVal Value As String)
ViewState("Text") = Value
End Set
End Property
The better way is to read viewstate members and only perform one type conversion operation. The viewstate write operation can be performed in batches before render. To meet this requirement, we can override the loadviewstate and saveviewstate methods to process the access actions of attributes and viewstate. After the control is initialized, The loadviewstate method is executed to load the control state restored by viewstate, before the control is render, The saveviewstate method is executed to save the final state of the control to viewstate. After this method, any changes made to the control will be ignored.
Instead of directly using viewstate to access attributes, we use "attribute region variable" to access attributes and load viewstate to attribute region variable during loadviewstate, when saveviewstate is used, the attribute region variable is written to viewstate. In this way, we rewrite the text attribute as follows.
Private FText As String
Property Text() As String
Get
Return FText
End Get
Set(ByVal Value As String)
FText = Value
End Set
End Property
''' <summary>
'''When the viewstate is enabled, the original control is disabled.
''' </summary>
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
If Not (savedState Is Nothing) Then
' Load State from the array of objects that was saved at vedViewState.
Dim myState As Object() = CType(savedState, Object())
If Not (myState(0) Is Nothing) Then
MyBase.LoadViewState(myState(0))
End If
If Not (myState(1) Is Nothing) Then
FText = CType(myState(1), String)
End If
End If
End Sub
''' <summary>
''' Controls the audience member into the viewstate.
''' </summary>
Protected Overrides Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveViewState()
Dim myState(1) As Object
myState(0) = baseState
myState(1) = FText
Return myState
End Function
Using the above method, we can load all the attribute values in the loadviewstate batch, while writing the attribute values in the saveviewstate batch, so that the type conversion operation is not always performed to Improve the efficiency.
Ii. Conclusion
Although attributes are generally stored in viewstate, you can consider not storing these attributes in viewstate if they are irrelevant or changed when they are not executed at all; because viewstate is a dual-edged edge, viewstate can easily help us maintain attribute values, but it also increases the volume of page transfers, therefore, you can decide whether to store the attribute in viewstate according to the actual situation.
Note: This article is also published in "the first it state help tie Ren Competition". If you think this article is helpful to you, remember to repeat it to improve your popularity ^
Http://ithelp.ithome.com.tw/question/10011745