This is a migrated thread and some comments may be shown as answers.

Preventing RadGrid from automatically recreating columns on postback

6 Answers 248 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dmw
Top achievements
Rank 1
dmw asked on 07 Oct 2010, 09:03 AM
Hello,

I noticed, due to the following error, that RadGrid attempts to automatically restore the columns of a grid from viewstate(?) on postback.

  Sys.WebForms.PageRequestManagerServerErrorException: Cannot create column with the specified type name: GridDateTimeRangeFilterColumn

I'd like to disable this functionality if possible, and continue to add the columns manually in OnInit. EnableViewState is set to false on the grid; I presumed that would be sufficient, but it appears to make no difference to this issue.

Telerik version is 2009.3.1314.35.

Thanks in advance.

6 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 12 Oct 2010, 12:45 PM
Hello Donna,

Please set the EnableColumnViewState property of the master table view to False, so that the grid knows that the columns may vary at some stage of the page lifecycle.
For additional information you can check out the Changing the grid structure dynamically on postback help topic.

I hope this helps.

Regards,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
dmw
Top achievements
Rank 1
answered on 12 Oct 2010, 03:22 PM
Thanks, EnableColumnsViewState seems to do the trick. However I wonder if it has any side effects - I noticed some problems with the toolbar I have embedded in the command bar of the grid after setting EnableColumnsViewState to false. Specifically, the contents of dynamically populated menu buttons disappear after postback. I guess that EnableColumnsViewState also controls viewstate for the command bar, is that correct?
0
Mira
Telerik team
answered on 14 Oct 2010, 03:55 PM
Hello Donna,

The state of the controls in the CommandItemTemplate is kept in the viewstate of the grid.
If the issue persist, please send us the code you use for populating the CommandItemTemplate and we will do our best to help you.

Greetings,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
dmw
Top achievements
Rank 1
answered on 19 Oct 2010, 11:23 AM
Here is a sample grid control which derives from RadGrid and includes a RadToolBar:
[ToolboxData("<{0}:MyGrid runat=server></{0}:MyGrid>")]
public class MyGrid : RadGrid
{
    private class ToolBarCommandItemTemplate : ITemplate
    {
        private readonly RadToolBar _toolBar;
 
        public ToolBarCommandItemTemplate(RadToolBar toolBar)
        {
            _toolBar = toolBar;
        }
 
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(_toolBar);
        }
    };
 
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
 
        MasterTableView.AutoGenerateColumns = false;
        MasterTableView.EnableColumnsViewState = false; // This appears to disable viewstate for the toolbar also.
        MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
        MasterTableView.CommandItemTemplate = new ToolBarCommandItemTemplate(ToolBar);
    }
 
    private RadToolBar _toolBar;
    public RadToolBar ToolBar
    {
        get
        {
            return _toolBar ?? (_toolBar = new RadToolBar());
        }
    }
 
    [DefaultValue(null)]
    [MergableProperty(false)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Editor("Telerik.Web.Design.ControlItemCollectionEditor, Telerik.Web.Design, Version=2009.3.1314.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4", typeof(UITypeEditor))]
    public RadToolBarItemCollection ToolBarItems
    {
        get
        {
            return ToolBar.Items;
        }
    }
};
Elsewhere we add a drop-down menu through markup:
<WebApplication2:MyGrid ID="MyGrid1" runat="server">
    <ToolBarItems>
        <telerik:RadToolBarDropDown runat="server" Text="My Menu">
        </telerik:RadToolBarDropDown>
    </ToolBarItems>
</WebApplication2:MyGrid>
... and then programmatically add menu items in OnLoad:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    if (!IsPostBack)
    {
        RadToolBarDropDown menu = MyGrid1.ToolBar.FindItemByText("My Menu") as RadToolBarDropDown;
        menu.Buttons.Add(new RadToolBarButton { Text = "Item 1" });
        menu.Buttons.Add(new RadToolBarButton { Text = "Item 2" });
        menu.Buttons.Add(new RadToolBarButton { Text = "Item 3" });
    }
}
After posting back, the programmatically-added toolbar buttons are lost. Note that EnableViewState is not disabled, only EnableColumnsViewState. In our real application there is quite a large amount of effort put into building up the toolbar so I would like to maintain viewstate for the toolbar, while disabling it for the column definitions.

I hope my sample makes sense.
0
Mira
Telerik team
answered on 25 Oct 2010, 03:25 PM
Hello Donna,

In order to implement the desired functionality, you should bind the toolbar on each Page_Init:
protected void Page_Init(object sender, EventArgs e)
{       
    RadToolBarDropDown menu = MyGrid1.ToolBar.FindItemByText("My Menu") as RadToolBarDropDown;
    menu.Buttons.Add(new RadToolBarButton { Text = "Item 1" });
    menu.Buttons.Add(new RadToolBarButton { Text = "Item 2" });
    menu.Buttons.Add(new RadToolBarButton { Text = "Item 3" });
}

I hope this helps.

Sincerely yours,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
dmw
Top achievements
Rank 1
answered on 28 Oct 2010, 11:46 AM
Thanks for your help, unfortunately there are also problems with our filter controls having disabled viewstate, so I think we will have to consider a different approach.

Thanks anyway.
Tags
Grid
Asked by
dmw
Top achievements
Rank 1
Answers by
Mira
Telerik team
dmw
Top achievements
Rank 1
Share this question
or