Have you written controls today? --ASP.net control development series (3)

Source: Internet
Author: User
Tags control label
Full attribute contact (1)

In the previous articles of this series, there were a few likes and a few likes, so I had enough enthusiasm. No, this article is coming soon ,:)
Hope to continue to receive encouragement and correction.
This time, we will discuss all aspects of Property Design in control development. The attributes are various. net language is the most basic syntax, but the control is a release that is provided to programmers for secondary development. Its powerful functionality and flexibility cannot be separated from good attribute design, therefore, I think attribute design is the first bastion in control development.
First, let's take a look at the HTML style code in the. aspx file:

<Asp: DropDownList id = "DropDownList1" runat = "server" Font-Bold = "True">
<Asp: ListItem Value = "1"> 1 </asp: ListItem>
<Asp: ListItem Value = "2"> 2 </asp: ListItem>
</Asp: DropDownList>

This type of code has been written countless times. However, we need to analyze this code from another perspective:
When the DropDownList control interacts with the user, it does not put its attributes together for representation, but is divided into the following situations:
1. Put IDs, Runat, and Font-Bold in the <> tag;
2. font-Bold is different from others. It has a short horizontal split into two parts. This representation is not a Font-Bold attribute name, but a sub-attribute, is the Bold attribute of the Font attribute (Font class), corresponding. the statement of the cs file is: Font. bold;
3. The ListItem is placed in the middle of the <> tag of the DropDownList pair, rather than the tag. Similarly, 1 and 2 are placed in the middle of the ListItem.
Next, let's take a look at how these attribute record methods are implemented.
There is an Attribute for this event PersistenceMode (System. Web. UI. PersistenceModeAttribute ).
It has the following values for selection:
Attribute (default) Property is saved in the label of the control; default value
The EnCodedInnerDefaultProperty attribute is encoded HTML and stored in the middle of the control tag pair. The above ListItem stores the Text attribute (1, 2) in this way );

[PersistenceMode (PersistenceMode. EnCodedInnerDefaultProperty)]
Public string Text
{
 
}

The InnerDefaultProperty attribute is stored in the middle of the control tag pair. The Items attribute of the DropDownList control is saved in this way (that is, the heap ListItem );

[PersistenceMode (PersistenceMode. InnerDefaultProperty)]
Public ListItem Items
{
Get
{

}
}

The InnerProperty attribute and other attributes are saved as nested content in the control label pair. The DataGrid uses this method to package a bunch of items in the tag. In this case, the attribute should be declared with the tag, containing the attribute value.

[PersistenceMode (PersistenceMode. InnerProperty)]
Public virtual TableItemStyle ItemStyle
{
Get
{
.
}
}

The second topic is the persistence of attributes.
As we all know, a webpage is based on the stateless mechanism. That is to say, after page Response, the status of the page content is not managed and saved. ASP.net provides the ViewState mechanism to save the page status, viewState is implemented by sending out an input type named _ ViewState in the page = 'den den '. That is to say, it is different from Session and saved in the page ...... no nonsense. Anyway, we need to save the status of the control (that is, its attributes), instead of losing it in the process of return. The solution we adopt is ViewState, the following is a simple example to illustrate the usage of ViewState in Property.

[
DefaultValue (2 ),
TypeConverter (typeof (Int32Converter )),
Category ("Behavior "),
Description ("Duration of complete once transition. (s )")
]
Public int Duration
{
Get
{
Object B = ViewState ["Duration"];
Return (B = null )? 2 int) B;
}
Set
{
If (value <1)
{
Throw new ArgumentOutOfRangeException ("Duration ");
}
ViewState ["Duration"] = value;
}
}

Directly Using ViewState in this way is the easiest way. We do not have to rewrite the LoadViewState and SaveViewState methods. As for how to customize ViewState management, we will introduce it in the following article.

There are too many things about attributes. This time, I will write about how to customize attributes to interact with IDE.

Related Article

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.