Once mentioned in ASP. NET Server Control for State management, you will naturally think of viewstate, for simple properties, this thing is simple after all, and when it comes to control status, we will talk about how to program this item separately, which is inconvenient. In fact, the so-called independent programming is a fixed segment. Code There is nothing complicated and troublesome, just how many extra letters are needed.
To put it simply, there are four steps:
1. Attribute declaration in the old way;
2. Tell the Runtime Library that you want to use the control status;
3. Save the control status;
4. Read the control status;
Let me give you a simple example of each step.
1. Attribute declaration in the old way;Buttontype type=Buttontype. Button;
PublicButtontype type
{
Set{This. Type=Value ;}
Get{Return This. Type ;}
}
String Text = " Button " ;
Public String Text
{
Set { This . Text = Value ;}
Get { Return This . Text ;}
}
2. Tell the Runtime Library that you want to use the control status;Protected Override VoidOninit (eventargs E)
{
Base. Oninit (E );
This . page. registerrequirescontrolstate ( This );
}
3. Save the control status;Protected Override ObjectSavecontrolstate ()
{
Queue<Object>States= NewQueue<Object>();
States. enqueue (type );
States. enqueue (text );
return states;
}
4. Read the control status. protected override void loadcontrolstate ( Object savedstate)
{< br> queue Object > States = savedstate as queue Object > ;
Type=(Buttontype) (States. dequeue ());
Text=(String) (States. dequeue ());
}
Add:
1. The data structure uses queue <t>. The first-in-first-out queue makes the Code look more comfortable.