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

RadGrid in composite user control

7 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marja
Top achievements
Rank 1
Marja asked on 02 Dec 2013, 12:31 PM
Hi,

I'm trying to use the RadGrid in a composite user control, where some of the features of the RadGrid are to be set dynamically by the user control (and the page the user control is placed on dictates its settings and appearance).

For example:
The user control is told which columns to present to the user and wether sorting is allowed, and then the user control will need to tell all that to the RadGrid so it can initialize itself with the proper columns and settings before databinding.

I'm struggling to make this work, as I don't know at which point in the life cycle I can get hold of the encapsulated RadGrid instance to set its options.
If I do that in the OnInit event of the user control, the RadGrid is not available yet. But as I understand, I need to define the RadGrid columns in its Init event, and I only know its required settings after the Init event has already passed.

I've tried placing a RadGrid declaratively in the user control, and I've also tried adding it programmatically to a placeholder, but I can't seem to get it working.

Can anyone give me any pointers? Thanks in advance!

7 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 05 Dec 2013, 08:08 AM
Hello Marja,

You can create public properties in the code-behind of the UserControl and use them on the main page when defining the custom control. These properties can be used on Page_Init or Page_Load of the UserControl to configure the grid settings.

An alternative approach would be to use the Page_Load or UserControl_Load on the main page to access the grid inside the control using the FindControl method and casting it to RadGrid. This will enable you to set grid properties depending on a condition from the main page.

A third option could be to access the main page from the UserControl using this.Parent or this.NamingContainer ( depends on your specific scenario ) and pass control values or public properties from the primary page to the UserControl to configure the grid.

Hope this helps. Please let us know which approach would be more convenient in your case so we can prepare a basic sample to aid you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Marja
Top achievements
Rank 1
answered on 05 Dec 2013, 09:17 AM
The problem is that RadGrid needs to define the columns to display in its Init event, but the information about which columns need to be displayed is coming from the user control.

The Init event for the user control fires before the init event for the RadGrid, but there's no chance to set properties for the user control before the RadGrid Init event is fired.
0
Marja
Top achievements
Rank 1
answered on 06 Dec 2013, 07:52 PM
Correction:
The Init event for the RadGrid fires before the Init event for the user control, so there's no chance to set properties for the RadGrid from the user control programmatically.

The RadGrid needs to define the columns to display in its Init event (eg. create GridBoundColumns), but at that point it hasn't been told yet which columns to display.
0
Marja
Top achievements
Rank 1
answered on 07 Dec 2013, 11:09 AM
I've read the information on http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html several times now. And I've made some progress, but I can't find a final working solution.

In my RadGrid I have a mix of GridBoundColumns and GridTemplateColumns.

I can't define the grid columns in the OnInit event, because at that point I don't have the required information yet (because of the container user control needing to initialise completely first).
So I need to define the RadGrid columns in the OnLoad event, as explained in http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html, section 1 ("Dynamically Defining the Structure of a Statically-declared Grid") and/or section 21 ("Creating a RadGrid on Page_Load").

This works fine for GridBoundColumns, but GridTemplateColumns are not persisted when sorting etc.
So when there's no reason for rebinding, and thus NeedDataSource is not called, those columns lose their display values and end up empty.

As I explained, I really can't create the GridTemplateColumns in OnInit, because at that point I don't know yet which columns I need.

Help...!
0
Eyup
Telerik team
answered on 10 Dec 2013, 08:53 AM
Hi Marja,

I have already replied to your query in your support ticket 766272.
I'm looking forward to the sample web site.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Marja
Top achievements
Rank 1
answered on 12 Dec 2013, 11:50 AM
Okay, I found my solution and I will try to explain what I did here in this thread for anyone else struggling with the same issue.

My scenario is like this:
I have a composite user control named wsCompositeRadGrid containing, among other things, a RadGrid instance.
The page where the wsCompositeRadGrid control is used tells the wsCompositeRadGrid which database table to use and which table columns to display. So in turn, the wsCompositeRadGrid needs to communicate this to the RadGrid so it can dynamically add the right columns.
Those RadGrid columns are not only of type GridBoundColumn, but there's also a column of type GridClientSelectColumn and often one or more columns of type GridTemplateColumn.

For the GridTemplateColumns to survive the page cycle, the entire RadGrid needs to be re-built on every postback, in the Init phase of the cycle. This is explained in detail at http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html.

In my previous post I explained I was struggling to find the point to hook into the page and control life cycle to be able to build and maintain the RadGrid on every postback.

The way I've got it working now is:
  1. I've created a subclass of the RadGrid, named wsRadGrid, so I can define additional properties and methods on it.
  2. The wsCompositeRadGrid is added declaratively to the page. It has a placeholder for a wsRadGrid instance to be added dynamically.
  3. The wsCompositeRadGrid properties are set in the Page_Init event of the Page, and immediately after that a method "BuildGrid" is triggered on the wsCompositeRadGrid (so still in Page_Init for the Page)
  4. This "BuildGrid" method dynamically creates a wsRadGrid instance, explicitly tells it to define its columns (by triggering a custom "DefineGridColumnsmethod), and then adds it to the placeholder.

This may seem simple now I've wrote it down, but it took me a quite some time to find the crucial steps in this process:
  • subclass RadGrid ==> wsRadGrid
  • define custom DefineGridColumns method on the subclass to define its columns
  • calling wsRadGrid.DefineGridColumns to dynamically define its columns after it has been instantiated by the wsCompositeRadGrid.

I hope this information may be of some use to others as well.

Best regards, Marja

0
Eyup
Telerik team
answered on 13 Dec 2013, 08:23 AM
Hi Marja,

Thank you for sharing your solution. I hope it will prove helpful to other developers as well.

However, please bear in mind that in case you are creating custom controls, this is out of our support team's scope even if they are inherited directly from RadControls.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Marja
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Marja
Top achievements
Rank 1
Share this question
or