简单的来说,分为四个步骤:
1、老办法声明属性;
2、告诉运行库你要使用控件状态;
3、保存控件状态;
4、读取控件状态;
每个步骤我举一个简单的例子,大家一看就明白了。
1、老办法声明属性;
ButtonType type
=
ButtonType.Button;
public ButtonType Type
{
set { this .type = value; }
get { return this .type; }
}
string text = " 按钮 " ;
public string Text
{
set { this .text = value; }
get { return this .text; }
}
public ButtonType Type
{
set { this .type = value; }
get { return this .type; }
}
string text = " 按钮 " ;
public string Text
{
set { this .text = value; }
get { return this .text; }
}
2、告诉运行库你要使用控件状态;
protected
override
void
OnInit(EventArgs e)
{
base .OnInit(e);
this .Page.RegisterRequiresControlState( this );
}
{
base .OnInit(e);
this .Page.RegisterRequiresControlState( this );
}
3、保存控件状态;
protected
override
object
SaveControlState()
{
Queue < object > states = new Queue < object > ();
states.Enqueue(type);
states.Enqueue(text);
return states;
}
{
Queue < object > states = new Queue < object > ();
states.Enqueue(type);
states.Enqueue(text);
return states;
}
4、读取控件状态;
protected
override
void
LoadControlState(
object
savedState)
{
Queue < object > states = savedState as Queue < object > ;
type = (ButtonType)(states.Dequeue());
text = ( string )(states.Dequeue());
}
{
Queue < object > states = savedState as Queue < object > ;
type = (ButtonType)(states.Dequeue());
text = ( string )(states.Dequeue());
}
补充一下:
1、数据结构这里采用了Queue<T>这个家伙,先进先出的队列,可以让代码看着舒服一些。