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;
}
}