Exemple #1
0
        protected override void RenderItemTemplateItems(IEnumerable source)
        {
            if (GroupItemTemplate != null)
            {
                var cachedResult = new List <KeyValuePair <String, RenderContext> >();

                foreach (var sourceItem in source)
                {
                    var cachedOutput = CurrentContext.CreateChildContext();
                    ItemTemplate.DataContext = sourceItem;
                    ItemTemplate.Render(cachedOutput);
                    cachedResult.Add(new KeyValuePair <string, RenderContext>(ItemTemplate.Group ?? "", cachedOutput));
                }

                var groupsList = cachedResult
                                 .Where(g => !String.IsNullOrEmpty(g.Key))
                                 .Select(g => g.Key)
                                 .Distinct()
                                 .ToList();

                if (groupsList.Count > 0)
                {
                    var template = GroupItemTemplate;
                    template.Children.Clear();
                    List <RenderContext> currentItems = new List <RenderContext>();
                    HtmlObjectDelegate   renderAction = (control) =>
                    {
                        foreach (var ctx in currentItems)
                        {
                            ctx.JoinToParent();
                        }
                    };

                    template.RenderContentEvent += renderAction;

                    foreach (var groupName in groupsList)
                    {
                        currentItems.Clear();
                        currentItems.AddRange(cachedResult
                                              .Where(g => g.Key == groupName)
                                              .Select(g => g.Value));
                        template.DataContext = groupName;
                        template.Render(CurrentContext);
                    }

                    template.RenderContentEvent -= renderAction;
                }
                else
                {
                    cachedResult.ForEach(c => c.Value.JoinToParent());
                }
            }
            else
            {
                foreach (var sourceItem in source)
                {
                    ItemTemplate.DataContext = sourceItem;
                    ItemTemplate.Render(CurrentContext);
                }
            }
        }
 public static T OnPostRender <T>(this T control, HtmlObjectDelegate handler) where T : HtmlObject
 {
     control.PostRenderEvent += handler;
     return(control);
 }
 public static T OnDataBind <T>(this T control, HtmlObjectDelegate handler) where T : HtmlObject
 {
     control.DataBindEvent += handler;
     return(control);
 }
 public static T OnRenderContent <T>(this T control, HtmlObjectDelegate handler) where T : HtmlObject
 {
     control.RenderContentEvent += handler;
     return(control);
 }