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. Override PerformDataBinding method 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 particular 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

相关文章:

  • 2021-10-21
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2021-07-11
  • 2021-09-06
猜你喜欢
  • 2021-10-14
  • 2021-06-24
  • 2021-07-24
  • 2022-12-23
  • 2022-01-20
  • 2022-12-23
相关资源
相似解决方案