Attribute application: simplifies ANF custom control Initialization

Source: Internet
Author: User

Attribute application to simplify the ANF custom control initialization process

I have benefited a lot from studying ANF source code. Many of these ideas are worth learning. Here, Bao Yu has already introduced how Asp. Net Forums implements code separation and skin replacement in Asp. Net Forums2.0 deep analysis. However, when there are many server-side controls in a custom control, the implementation code of the InitializeSkin method is a little annoying. For example, let's take a look at the AdminSiteSettings code. It is really annoying. The modes are the same, such as TextBox DisableSiteReason = skin. FindControl ("DisableSiteReason") as TextBox. So today we want to use Attribute to simplify it.

First, we need to add an Attribute class, which is currently called BindControlAttribute:

Using System; using System. collections. generic; using System. text; using System. reflection; namespace AspNetForums. controls {[AttributeUsage (AttributeTargets. field | AttributeTargets. property, AllowMultiple = false)] class bindcontrolattriple: Attribute {string _ ctrlID; public BindControlAttribute (string ctrlID) {_ ctrlID = ctrlID ;} public string ControlID {get {return _ ctrlID ;}}}}

This type of Attribute function is relatively simple, that is, let this Attribute record the ID of the control to which the field is bound.

The second step is to modify SkinnedForumWebControl, mainly to add a method:

Private void InitializeFields (Control skin) {FieldInfo [] fields = this. getType (). getFields (BindingFlags. nonPublic | BindingFlags. instance | BindingFlags. declaredOnly); foreach (FieldInfo fi in fields) {if (fi. isDefined (typeof (BindControlAttribute), false) {BindControlAttribute bind = fi. getCustomAttributes (typeof (bindcontrolattrites), false) [0] as BindControlAttribute; object ctrl = skin. findControl (bind. controlID); fi. setValue (this, ctrl );}}}

Actually in fi. isDefined (typeof (bindcontrolattried), false) Here I want to add a condition, that is, the type of the restricted field is System. web. UI. control or its subclass, but failed to succeed after trying several methods. Please give me some advice...
Remember to reference the System. Reflection namespace and change the implementation of the CreateChildControls method:

Protected override void CreateChildControls () {Control skin = null; if (inlineSkin! = Null) {inlineSkin. instantiateIn (this); InitializeSkin (this);} else {// Load the skinskin = LoadSkin (); // Initialize the fieldsInitializeFields (skin ); // Add this line // Initialize the skinInitializeSkin (skin); Controls. add (skin );}}

At this point, the implementation task is complete. The following is the application. The application is relatively simple. You only need to add this BindControlAttribute when defining the field. For example:

Uing System; using System. collections; using System. web; using System. web. UI; using System. web. UI. webControls; using AspNetForums. components; using AspNetForums. enumerations; namespace AspNetForums. controls {// server control of the Forum Group list /// public class ForumGroupView: SkinnedForumWebControl {# region member FIELD private ForumContext forumContext = ForumContext. current; private string skinFilename = "View-ForumGroupView.ascx "; [BindControl ("forumGroupRepeater")] // Add this private Repeater repeater when defining a field; # endregion public ForumGroupView () {// Assign a default template nameif (SkinFilename = null) skinFilename = skinFilename ;} # endregion # region control initialization //******************************** *********************************** // Initializeskin ////// Initializes the user control loaded in CreateChildControls. initialization // cons Ists of finding well known control names and wiring up any necessary events. ////////******************************** * **********************************/protected override void initializeSkin (Control skin) {// repeater = (Repeater) skin. findControl ("forumGroupRepeater"); // This line is useless. DataBind ();}

Is it better?

It took me a lot of effort to use reflection for the first time (mainly because I didn't read the book carefully :-)). I tried to obtain private fields many times before trying it out. BindingFlags seems to have to be added in the next three years, but none of them will work. In addition, I found that the Attribute constructor is called only when the GetCustomAttributes function is called. I don't know if there is any second class?

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.