locked
List page template column generator problem RRS feed

  • Question

  • User539648229 posted

    Hi

    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:

    http://forums.asp.net/t/1678747.aspx?Failed+to+load+viewstate+

    http://forums.asp.net/t/1353129.aspx?Failed+to+load+viewstate

    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.

    Thanks

    Thursday, July 17, 2014 12:57 AM

All replies

  • User-330204900 posted

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

    Thursday, July 17, 2014 6:41 AM
  • User539648229 posted

    Hi

    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);
                        else
                            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);
                    fields.Add(field);
                }
    
                if (table.PrimaryKeyColumns.Count > 0)
                {
                    DynamicField commandField = getCommandField();
                    commandField.ItemStyle.Width = new Unit(11, UnitType.Em);
                    fields.Add(commandField);
                }
    			
                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;
            }
        }

    Thanks

    Friday, July 18, 2014 12:03 AM
  • User-330204900 posted

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

    Friday, July 18, 2014 2:39 AM
  • User539648229 posted

    Hi Stephen, It still does not work.

    Saturday, July 19, 2014 7:03 AM
  • User-330204900 posted

    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.

    Sunday, July 20, 2014 7:55 AM
  • User539648229 posted

    Thanks

    Tuesday, July 22, 2014 1:02 AM
  • User-330204900 posted

    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

    Tuesday, July 22, 2014 4:16 AM
  • User539648229 posted

    OK

    Friday, July 25, 2014 3:11 PM
  • User539648229 posted

    Hi Steve

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

    Monday, September 15, 2014 1:14 PM
  • User-330204900 posted

    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.

    Tuesday, September 16, 2014 5:13 AM
  • User539648229 posted

    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

    Wednesday, September 17, 2014 2:23 AM
  • User-330204900 posted

    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.

    Wednesday, September 17, 2014 4:43 AM
  • User539648229 posted

    Thanks

    Wednesday, September 17, 2014 6:18 AM