ASP.net 3.5 control and Component Development Technology series-server control properties (ii)

Source: Internet
Author: User
Tags tagname

4.4.3.2 use ControlBuilder to parse complex content

Customizing the page parsing logic with the System.Web.UI.ControlBuilder class allows you to customize any type of markup, rather than qualifying a child tag as a child control, as you would override the Addparsesubobject method, and must have a prefix and a runat property, the following Use an example directly to illustrate this type of usage.

First, two files ScriptItem.cs and ScriptItemCollection.cs are established, respectively defining the Scriptitem class and the Scriptitemcollection class. Where the Scriptitem class primarily stores user-defined client script commands (JavaScript blocks), scriptitemcollection can define a collection container, each of which is a Scriptitem item. is very similar to the previous set implementation. The complete code for these two classes is as follows:

1. Scriptitem class

/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>

private string _Text;
[DefaultValue("")]
[Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design", typeof (UITypeEditor))]
[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
[NotifyParentProperty(true)]
/// <summary>
/// JavaScript脚本块
/// </summary>
public string Text
{
   get
   {
     return _Text;
   }
   set
   {
     _Text = value;
   }
}

The text in this class is used to store user-defined script blocks; the Editor metadata attribute specifies that the editor of the Text property in the Properties window is a drop-down block input editor, which is explained in detail in the next section of the property editor, which is only known for its functionality.

Note that in the previous section, when using the AddParsedSubObject implementation page to parse the child controls, the child tags of the three collections to be nested: ListItem, ListItem2, ListItem3 inherit the control base class, which is intended to be labeled as is a child control (it also has a prefix and runat property), and the scriptitem here does not inherit any base classes, thus avoiding the inheritance of redundant properties and methods in some base classes.

2. Scriptitemcollecton class

/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>

[ToolboxItem(false)]
public class ScriptItemCollection : List<ScriptItem>
{
   public ScriptItemCollection() : base() { }
   public ScriptItemCollection(int capacity) : base(capacity) { }
   public ScriptItemCollection(IEnumerable<ScriptItem> collection):base  (collection) { }
}

After defining these two classes, implement our own ControlBuilder class, you can directly inherit the class and implement your own method, and the predefined constructor class code is as follows:

/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>

public class ScriptItemBuilder : ControlBuilder
{
   public override Type GetChildControlType(string tagName, IDictionary  attributes)
   {
     if (string.Compare(tagName.ToLower(), "scriptitem", false, CultureInfo.  InvariantCulture) == 0)
     {
       return typeof(ScriptItem);
     }
     return null;
   }
   public override bool AllowWhitespaceLiterals()
   {
     return false;
   }
}

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.