List page template column generator problem


I use column generator in List page template to generate command column, hide some columns, set column alignment and size, etc(with some attribute I defined). There is a problem about ViewState and some entities sort and paging functionality causes the error
that contains:

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back
must match the type and position of the controls added during the initial request.

Based on the following threads I think the problem is the timing of setting column generator:

I set column generator in Load event of List page template because in Init event of Page and GridView It has no effect. The solution provided in the first thread does not work becuase I do more than hiding columns and the second solution is for prior 4.0
.Net framework. Any solution to resolve the issue.


Hi ali, I use column generators all the time and have no issues could you post your code please?


The code follows. There are some extension methods that hopefully are clear to understand:

    public class CustomizedAutoFieldGenerator : IAutoFieldGenerator
        private MetaTable table;
        public IEnumerable<MetaColumn> SkipList { get; set; }
        public CustomizedAutoFieldGenerator(MetaTable metaTable)
            table = metaTable;
            SkipList = table.Columns.GetSkipList(PageActionType.List);
        public ICollection GenerateFields(Control control)

            var filteredSortedColumns = table.Columns.Where(c => c.IncludeField(SkipList))
                                                     .OrderBy(c => c.ColumnOrdering())
                                                     .ThenBy(c => c.Name);
            List<DynamicField> fields = new List<DynamicField>();

            foreach (MetaColumn column in filteredSortedColumns)
                Unit unit = column.GetWidth();
                if (unit == Unit.Empty)
                    object columns = column.GetUIHintParameter("Columns");
                    if (columns != null)
                        unit = new Unit((int)columns, UnitType.Em);
                        unit = new Unit(Math.Max(1, column.ColumnType.GetColumnCount() / 2), UnitType.Em);

                DynamicField field = new DynamicField()
                    DataField = column.Name,
                    HeaderText = column.ShortDisplayName,
                    SortExpression = getSortExpresion(column)
                field.ItemStyle.Width = unit;

                if (column.ColumnType.IsBoolean())
                    field.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                field = applyUIHintParameters(column, field);

            if (table.PrimaryKeyColumns.Count > 0)
                DynamicField commandField = getCommandField();
                commandField.ItemStyle.Width = new Unit(11, UnitType.Em);
            return fields;
        private string getSortExpresion(MetaColumn column)
            if (column.UIHint == "Image")
                return string.Empty;
            return column.Name;

        private DynamicField applyUIHintParameters(MetaColumn column, DynamicField control)
            object[] parameters = column.GetUIHintParameters(new string[] { "HorizontalAlign", "VerticalAlign" });
            if (parameters[0] != null)
                control.ItemStyle.HorizontalAlign = (HorizontalAlign)parameters[0];
            if (parameters[1] != null)
                control.ItemStyle.VerticalAlign = (VerticalAlign)parameters[1];

            return control;
        private DynamicField getCommandField()
            // get the first primary key field                
            DynamicField ctrl = new DynamicField();
            ctrl.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            ctrl.HeaderText = Resources.General.Commands;
            ctrl.DataField = table.PrimaryKeyColumns[0].Name;
            ctrl.SortExpression = string.Empty;
            ctrl.UIHint = "GridCommand";
            return ctrl;


Hi Ali, I suspect it’s the command field that is causing the issue I suggest commenting that out an testing.

Hi Stephen, It still does not work.

I’ll have to try running here in a sample project, as I don’t use field generators for my project any more I only use them to set the item style wrap on columns I do all my field filtering in custom meta models. An I see you are doing some clever stuff in
there I’ll look late next week.


Sadly, I’m going to be away next week now so it will have to wait until I get back if I can’t look by Thursday


Hi Steve

I think there is no way except reimplementing List Page template with ListView. What’s your opinion?

Sorry Ali, I must be being a little Dumb I think the issue is the Command Column try removing that from the Field Generator and see what happens. I remember I have tried moving the Command column this way and had issues in the end I gave up and I only use
the IAutoFieldGenerator to handle Column Wrap and other formatting. I use a custom Metamodel to do field and Filter ordering etc.

The problem was that I need command field and it didn’t work until I transferred field generator from Init event to Load event and then there was the problem of ViewState. Provided this situation I think there is no way except using ListView in list templte.
I saw your MetaModel approach but it does not work with my other requirements.

Thanks anyway

OK I got you and see what you need, there may be a way around this but at the moment I’m not sure but will get back to you if I find a solution.


Leave a Reply