繼承 ControlBuilder,覆寫 GetChildControlType 根據標籤名稱回傳對應型別 (ScriptItem),AllowWhitespaceLiterals 回傳 false 忽略空白。
public class ScriptItemBuilder : ControlBuilder {
public override Type GetChildControlType(string tagName, IDictionary attributes) {
return tagName.ToLower() == "scriptitem" ? typeof(ScriptItem) : null;
}
public override bool AllowWhitespaceLiterals() { return false; }
}
<cc1:MyControl runat="server">
<ScriptItem>alert('Hello');</ScriptItem>
<ScriptItem>console.log('World');</ScriptItem>
</cc1:MyControl>
不需子標記繼承 Control、加前綴或 runat=server,支援任意 XML 結構。