将分组模板添加到ASP:在任何标题模板之后显示的Repeater

时间:2021-06-01 07:36:16

I have sub classed a asp repeater similar to A Grouping Repeater All works fine apart from if I also have a <HeaderTemplate> </HeaderTemplate> The grouping template is rendered before the header template.

我有一个类似于A Grouping Repeater的asp转发器,除了我还有一个 分组模板在头模板之前呈现之外,所有工作都很好。

I would really like to either be able to choose the order in which the templates are rendered or just have the <GroupTemplate> Rendered after the header.

我真的想要能够选择模板渲染的顺序,或者只是在标题后面有 渲染。

1 个解决方案

#1


0  

Did you try the simplier one? we cant evaluate you bug, if we cant see the code that you re running. Please post examples.

你尝试过简单的吗?如果我们看不到您正在运行的代码,我们无法评估您的错误。请发布示例。

The simplier one from the comments:

评论中更简单的一个:

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;


    /// <summary> 
    /// This wraps the Web Repeater class and adds grouping capabilities.
    /// </summary> 
    public class GroupingRepeater : Repeater
    {

        private string _lastGroup;
        private string _groupColumn;
        private ITemplate _groupTemplate;

        /// <summary> 
        /// Gets or sets the column to group by.
        /// </summary> 
        [Browsable(true), Category("Behaviour")]
        public string GroupColumn
        {
            get { return _groupColumn; }
            set { _groupColumn = value; }
        }

        /// <summary> 
        /// Gets or sets the template used for Grouping.
        /// </summary> 
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        [TemplateContainer(typeof(RepeaterItem))]
        [DefaultValue("")]
        public virtual ITemplate GroupTemplate
        {
            get { return _groupTemplate; }
            set { _groupTemplate = value; }
        }

        /// <summary> 
        /// Default constructor.
        /// </summary> 
        public GroupingRepeater()
        {
            _lastGroup = null;
        }

        /// <summary> 
        /// Called when an item is created.
        /// </summary> 
        /// <param name="e"></param> 
        protected override void OnItemCreated(RepeaterItemEventArgs e)
        {
            base.OnItemCreated(e);

            if (!string.IsNullOrEmpty(GroupColumn))
            {
                string currentGroup = e.Item.DataItem.GetType().GetProperty(GroupColumn).GetValue(e.Item.DataItem, null).ToString();
                if (_lastGroup == null || _lastGroup != currentGroup)
                {
                    RepeaterItem item = new RepeaterItem(0, ListItemType.Item);

                    GroupTemplate.InstantiateIn(item);
                    item.DataItem = e.Item.DataItem;
                    this.Controls.Add(item);
                    item.DataBind();

                    _lastGroup = currentGroup;
                }
            }
        }

    }

#1


0  

Did you try the simplier one? we cant evaluate you bug, if we cant see the code that you re running. Please post examples.

你尝试过简单的吗?如果我们看不到您正在运行的代码,我们无法评估您的错误。请发布示例。

The simplier one from the comments:

评论中更简单的一个:

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;


    /// <summary> 
    /// This wraps the Web Repeater class and adds grouping capabilities.
    /// </summary> 
    public class GroupingRepeater : Repeater
    {

        private string _lastGroup;
        private string _groupColumn;
        private ITemplate _groupTemplate;

        /// <summary> 
        /// Gets or sets the column to group by.
        /// </summary> 
        [Browsable(true), Category("Behaviour")]
        public string GroupColumn
        {
            get { return _groupColumn; }
            set { _groupColumn = value; }
        }

        /// <summary> 
        /// Gets or sets the template used for Grouping.
        /// </summary> 
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        [TemplateContainer(typeof(RepeaterItem))]
        [DefaultValue("")]
        public virtual ITemplate GroupTemplate
        {
            get { return _groupTemplate; }
            set { _groupTemplate = value; }
        }

        /// <summary> 
        /// Default constructor.
        /// </summary> 
        public GroupingRepeater()
        {
            _lastGroup = null;
        }

        /// <summary> 
        /// Called when an item is created.
        /// </summary> 
        /// <param name="e"></param> 
        protected override void OnItemCreated(RepeaterItemEventArgs e)
        {
            base.OnItemCreated(e);

            if (!string.IsNullOrEmpty(GroupColumn))
            {
                string currentGroup = e.Item.DataItem.GetType().GetProperty(GroupColumn).GetValue(e.Item.DataItem, null).ToString();
                if (_lastGroup == null || _lastGroup != currentGroup)
                {
                    RepeaterItem item = new RepeaterItem(0, ListItemType.Item);

                    GroupTemplate.InstantiateIn(item);
                    item.DataItem = e.Item.DataItem;
                    this.Controls.Add(item);
                    item.DataBind();

                    _lastGroup = currentGroup;
                }
            }
        }

    }