Design of the Collection [set] attribute of the Asp.net Server Control for component development-editing and saving

Source: Internet
Author: User
Collection Property is often used when you create a server control. I will summarize some of the learned knowledge points in my development, which may be helpful to beginners.
If you have developed controls, you must know that if the properties of your controls are basic class data types, you do not need to save and restore these properties ,. net has completed these support work. however, if you want to add a set property to the control, it's not that easy! Set attributes are very common, such as ListItemCollection in ListBox and GridViewColumn in GridView.
Below, I will list some key points in development:
1. Set the following properties for the Control Level Control Class Level:
[ParseChildren (true)]
Public class MyControl: WebControl
2. Property Level settings are as follows:
[DesignerSerializationVisibility (DesignerSerializationVisibility. Content)]
[PersistenceMode (PersistenceMode. InnerProperty)]
[Editor (typeof (MyCollectionEditor), typeof (System. Drawing. Design. UITypeEditor)]
Public MyCollection Items
{
Get {}
}
Note: All set attributes should be read-only!
3. Implement MyCollectionEditor.

MyCollectionEditor
Class MyCollectionEditor: CollectionEditor
{

Public MyCollectionEditor (Type type): base (type)
{
}

Protected override bool CanSelectMultipleInstances ()
{
Return false;
}

Protected override System. Type CreateCollectionItemType ()
{
Return typeof (MyItem );

}
Protected override string GetDisplayText (object value)
{
MyItem exp = value as MyItem
// Return exp. ToString ();
Return exp. DisplayText;
}

}

As you can see, it is not difficult to implement this. The basic class CollectionEditor in the Framework already provides us with the 99% feature. We just need to customize it! Note that the first three processes are indispensable. The last one is to display the project in the edit dialog box. It can be a random string.

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.