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

[Solved] Empty GridColumns after PostBack

6 Answers 250 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ferdinand
Top achievements
Rank 1
Ferdinand asked on 08 Jan 2010, 12:29 PM
Hi
I got a problem with a dynamically created RadGrid.
My definition is the following:
protected void Page_Init(object sender, EventArgs e) 
        { 
            createGrid("rgSchedules""TK_ID"truethis.phSchedulesGrid, out this.rgSchedules); 
            createSchedulesGridColumns(this.rgSchedules); 
            this.rgSchedules.SelectedIndexChanged += new EventHandler(rgSchedules_SelectedIndexChanged);             
        } 
 
        void rgSchedules_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            // nothing happens but PostBack for test purposes 
        } 
 
        private void createGrid(string gridId, string datakeyName, bool autoPostBack, PlaceHolder ph, out RadGrid localGrid) 
        { 
            RadGrid grid = new RadGrid(); 
            grid.DataSourceID = "odsSchedules"
            grid.EnableViewState = false
            grid.ID = gridId; 
            grid.BorderStyle = BorderStyle.None; 
            grid.AutoGenerateColumns = false
            grid.AllowSorting = true
            grid.AllowPaging = false
            grid.AllowMultiRowSelection = false
            grid.Height = 450; 
            grid.Width = 450; 
            grid.GridLines = GridLines.None; 
            grid.PagerStyle.Visible = false
            grid.ClientSettings.EnablePostBackOnRowClick = autoPostBack; 
            grid.ClientSettings.EnableRowHoverStyle = true
            grid.ClientSettings.Selecting.AllowRowSelect = true
            grid.ClientSettings.Scrolling.AllowScroll = true
            grid.ClientSettings.Scrolling.UseStaticHeaders = true
            grid.ClientSettings.Scrolling.SaveScrollPosition = true
            grid.ClientSettings.Resizing.AllowColumnResize = true
            grid.ClientSettings.Resizing.ClipCellContentOnResize = false
            grid.ClientSettings.Resizing.EnableRealTimeResize = true
            grid.ClientSettings.Resizing.ResizeGridOnColumnResize = true
            grid.MasterTableView.DataKeyNames = new string[] { datakeyName }; 
            grid.MasterTableView.OverrideDataSourceControlSorting = true
            localGrid = grid; 
            ph.Controls.Clear(); 
            ph.Controls.Add(grid); 
        } 
        private void createSchedulesGridColumns(RadGrid grid) 
        { 
            grid.Columns.Clear(); 
 
            string nameCaption = this.GetLocalResourceObject("appointment").ToString(); 
 
            DynamicGrid.AddBoundFieldToGridView("TK_BEZEICHNUNG", nameCaption, grid); 
            DynamicGrid.AddDateTimeFieldToGridView("TK_ANFANG"this.GetLocalResourceObject("begin").ToString(), 
                grid); 
        } 
 

As you can see the grid performs a PostBack on click on a row. So if the grid or another control on the page performs a PostBack the grid gets some new, completly empty columns. They are exactly as many as the defined ones (in our case 2).

Did I miss anything? What am I doing wrong?

Best regards
Ferdinand

6 Answers, 1 is accepted

Sort by
0
Johny
Top achievements
Rank 1
answered on 08 Jan 2010, 01:12 PM
Hi Ferdinand,

You can review the following document referring programmatic grid creation. Please mind that the approach is different depending on whether the grid is declared in the markup or crated entirely in the code-behind:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

Thanks,
Johny


0
Ferdinand
Top achievements
Rank 1
answered on 08 Jan 2010, 01:35 PM
Hi Johny,
thanks for the fast reply. The example you posted was exact the one I used to create the grid. And unfortunately I cannot find any difference between my grid and the one in the help document. Futhermore it is not possible to declare the grid statically because I use template columns in the original version which I left out here to keep it as simple as possible.
Any ideas?

Best regards
Ferdinand
0
Martin
Telerik team
answered on 08 Jan 2010, 03:35 PM
Hello Ferdinand,

I would suggest you to modify your createGrid() method like this:

private void createGrid(string gridId, string datakeyName, bool autoPostBack, PlaceHolder ph, out RadGrid localGrid)
    {
        ...
        grid.EnableViewState = false;
        grid.MasterTableView.EnableColumnsViewState = false;
        ...
    }

Please also note that according our documentation when the grid is created entirely in the code-behind, the columns should be added to the GridTableView before the grid is placed in the placeholder.

I hope this helps,
Martin
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ferdinand
Top achievements
Rank 1
answered on 11 Jan 2010, 09:15 AM
Hi Martin and thanks for the answer, this really helped me out. There is just one thing that I do not understand yet: How do I preserve the state (especially the selected rows) of the grid if I always rebuild it?

Any help would be appretiated.

Best regards
Ferdinand
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Jan 2010, 11:05 AM
Hi Ferdinand,

Here are a few help document links which you can refer to persist selection in a grid. Probably any of these can help you out:
Persisting the selected rows client-side on sorting/paging/filtering/grouping
Persisting the selected rows server-side on sorting/paging/filtering/grouping

Thanks
Princy.
0
Ferdinand
Top achievements
Rank 1
answered on 11 Jan 2010, 02:37 PM
Thanks Princy, I found my mistake. I did not reset the state in PreRender event handler.

Best regards
Ferdinand
Tags
Grid
Asked by
Ferdinand
Top achievements
Rank 1
Answers by
Johny
Top achievements
Rank 1
Ferdinand
Top achievements
Rank 1
Martin
Telerik team
Princy
Top achievements
Rank 2
Share this question
or