自訂控制項開發2–控制項開發

來源:互聯網
上載者:User

首先看段代碼

 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty),
        NotifyParentProperty(true)]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        [NotifyParentProperty(true)]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

會發現Value屬性,並沒有標誌PersistenceMode屬性,那麼它的預設屬性就是PersistenceMode.Attribute. 由於我們設定了Text為預設屬性,所以當我們將Value也設定為InnerDefaultProperty的時候,Value的值沒被呈現。原因是我們:

 

    [ParseChildren(true,"Text")]
    public class MyItem

 標記了MyItem類為預設屬性.

 

    [ParseChildren(true, "Items")]
    [ControlBuilder(typeof(ScriptItemBuilder))]
    public class MyItemsControl : WebControl, INamingContainer
    {
        private MyItems _items;
        [PersistenceMode(PersistenceMode.InnerDefaultProperty),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
        ]
        public MyItems Items
        {
            get
            {
                if (_items == null)
                {
                    _items = new MyItems();
                } return _items;
            }
        }

這裡我自訂了ControlBuilder來編輯Items集合。它可以解析並返回任意的類型,而我們前面提到的AddParsedSubObject只能解析標記有runat='Server'的控制項。

而ControlBuilder可以解析任意標籤。

    public class MyItemerBuilder : ControlBuilder
    {
        public override Type GetChildControlType(string tagName, IDictionary attribs)
        {
            if (string.Compare(tagName.ToLower(), "MyItem", false, CultureInfo.InstalledUICulture) == 0)
            {
                return typeof(MyItem);
            }
            return null;
        }
    }

DownLoad

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.