6.2.4 implementation of custom type view state
The concepts and principles of the previous view state have been made clear, and this section illustrates how viewstate works in one instance. Create a Web custom control Viewstatecontrol that inherits from WebControl or control, and the code is as follows:
///<summary>
///get more from this book, see:
///http://blog.csdn.net/ChengKing/archive/2008/08/18/ 2792440.aspx
///</summary>
[ToolBoxData ("<{0}:viewstatecontrol runat=server></{0}: Viewstatecontrol> ")]
public class Viewstatecontrol:webcontrol
{
}
because WebControl is inherited from the control base class , the control already has the ability to perform methods LoadViewState and saveviewstate in the view phase of the control life cycle. Add three different types of properties to the control, as follows:
///<summary>
///get more from this book, see:
///http://blog.csdn.net/ChengKing/archive/ 2008/08/18/2792440.aspx
///</summary>
private string _text;
[Bindable (True)]
[DefaultValue (")]
[Localizable (true)]
[Category (Test View state)]
[Description (" No view state store is used ")]
Public String Text_noviewstate
{
Get
{
return _text;
Set
{
This._text = value;
}
[Bindable (True)]
[DefaultValue (")]
[Localizable (true)]
[Category (Test View state)]
[ Description ("Use the ViewState property to saveStored data this property ")]
public string text_viewstate
{
Get
{
String s = (string) viewstate[" Text_viewstate "];
Return ((s = = null)? STRING.EMPTY:S);
}
Set
{
viewstate["text_viewstate"] = value;
}
}
Private Facestyle _facestyle;
[PersistenceMode (Persistencemode.innerproperty)]
[DesignerSerializationVisibility (designerserializationvisibility.content)]
[Notifyparentproperty (True)]
[Category (Test View state)]
[Description (Custom view state (implementing the IStateManager Interface) stores this property)]
public Facestyle facestyle
{
Get
{
if (_facestyle = null)
{
_facestyle = new Facestyle ();
if (istrackingviewstate)
{
(IStateManager) _facestyle). TrackViewState ();
}
Return _facestyle
}
}