Databoundcontrol support template [It is similar with Repeater control]

Source: Internet
Author: User

The purpose of this essay is recording the key steps if you hope to create control and hope it support template and edit the templates at designer time.

1. create a class derives from control class, inamecontainer, idataitemcontainer. this step is a little same as repeatitem (it is an instance to bind data that you can iterate datasource ). such as follows:

   [ToolboxItem(false)]    public class myItem :Control, IDataItemContainer    {        private object _currentItem;        private int _itemIndex;        public myItem(int itemIndex,object item)        {            _itemIndex = itemIndex;            _currentItem = item;        }        #region IDataItemContainer Members        public object DataItem        {            get { return _currentItem; }        }        public int DataItemIndex        {            get { return _itemIndex; }        }        public int DisplayIndex        {            get { return _itemIndex; }        }        #endregion    }

2. OverridePerformdatabindingMethod to bind data from datasource and present the data with the instance of myitem class.

  protected override void PerformDataBinding(IEnumerable data)        {            this.Controls.Clear();                createTemplateControls(TemplateType.HeaderTemplate, -1, null);                if (DataSource != null)                {                    IEnumerator iList = data.GetEnumerator();                    int index = 0;                    while (iList.MoveNext())                    {                        myItem itemObj = createTemplateControls(TemplateType.ItemTemplate, index, iList.Current);                        ItemEventArgs arg = new ItemEventArgs(itemObj);                        this.OnDataBound(arg);//Raise Event                        itemObj.DataBind();//Raise DataBind() so that all the controls in Template support databinder.                        index += 1;                    }                }                createTemplateControls(TemplateType.FootTemplate, -2, null);        }             private myItem createTemplateControls(TemplateType type,int itemIndex,object currentItem)        {            ITemplate template = null;            switch (type)            {                case TemplateType.HeaderTemplate:                    template = HeaderTemplate;                    break;                case TemplateType.ItemTemplate:                    template = ItemTemplate;                    break;                case TemplateType.FootTemplate:                    template = FootTemplate;                    break;                default:                    template = HeaderTemplate;                    break;            }            myItem headItem = null;            if (template != null)            {                headItem = new myItem(itemIndex, currentItem);                template.InstantiateIn(headItem);                this.Controls.Add(headItem);            }            return headItem;        }    }

3. To support editable template at designer time, we 'd better to create particle controldesigner.

 public class MyRepeaterDesigner : DataBoundControlDesigner    {        public override void Initialize(IComponent component)        {            base.Initialize(component);            SetViewFlags(ViewFlags.TemplateEditing, true);        }        public override string GetDesignTimeHtml()        {            MyRepeater control = Component as MyRepeater;            if (control != null)            {                if (control.ItemTemplate == null)                {                    return CreatePlaceHolderDesignTimeHtml("You can go through the 'Righ click' or click the 'Smart tag' to edit 'Item/Header/Foot template '");                }            }            return base.GetDesignTimeHtml();        }        TemplateGroupCollection templateGroups;        public override TemplateGroupCollection TemplateGroups        {            get            {                if (templateGroups == null)                {                    templateGroups = base.TemplateGroups;                    TemplateGroup head_template = new TemplateGroup("HeaderTemplate");                    head_template.AddTemplateDefinition(new TemplateDefinition(this, "Header", Component, "HeaderTemplate", false));                    templateGroups.Add(head_template);                    TemplateGroup item_template = new TemplateGroup("ItemTemplate");                    item_template.AddTemplateDefinition(new TemplateDefinition(this, "Item", Component, "ItemTemplate", false));                    templateGroups.Add(item_template);                    TemplateGroup foot_template = new TemplateGroup("FootTemplate");                    foot_template.AddTemplateDefinition(new TemplateDefinition(this, "Foot", Component, "FootTemplate", false));                    templateGroups.Add(foot_template);                }                return templateGroups;            }        }    }

Download

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.