Use the control status in ASP. NET Server Control

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.