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

Columns dynamic creation

9 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pirobox
Top achievements
Rank 1
pirobox asked on 07 Sep 2010, 04:57 PM
Hi
sorry for bothering, but I've a few problems with the programmatic creation of columns in rad grid.

I start from the scenario that for me it would be perfect because I'd like to understand how far the grid is from it:
- should be possible to create the columns of the grid in the page_load (when IsPostBack == false)
- the dynamic columns should be of type Template
- during the event phase of the page (basically when a user clicks a button) it should be possible to clear the old columns and create new ones.

I've looked to this example
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/groupby/defaultcs.aspx
where you attach columns dynamically on the onload of the page. That works with simple columns, but fails with templated columns.
For precision's sake, I'm using NeedDataSource event, but on postbacks the InstantiateIn method of the templates is not called.
Is there any way to force it on postbacks?
Should I use the simple DataBind approach?

I've also tried creating the columns in the oninit (I underline the fact that I don't like it very much, because OnInit is a very early phase of the control lifecycle. Many informations could not be there for deciding which column instantiate).
I was able to create both simple and template columns, but I had to do one of these choices:
1. Check if IsPostBack == false and leave EnableColumnsViewState == true
2. Don't check the IsPostBack property and set EnableColumnsViewState = false
Which is the correct approach?

May you give some advice on how to fulfill my scenario?

Best regards, Andrea Pirola

9 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 08 Sep 2010, 07:56 AM
Hello Andrea,

Note that template columns must be added in the Page_Init event handler, so that the template controls can be added to the ViewState. Review the help article below which elaborates on this subject:
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html#Creating_template_columns_programmatically

I hope this gets you started properly.

Sincerely yours,
Pavlina
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
pirobox
Top achievements
Rank 1
answered on 08 Sep 2010, 10:25 AM
Hi,
(I apologize for the following rudeness, but I'm quite frustrated now)
I had already read that article and I had found it very unsatisfactory.
Let me summarize so you can tell me your opinion.
In that article you say that there are two ways to create a grid:
1. Dynamically defining the structure of a statically-declared grid
this means that I should work in the OnLoad event verifying IsPostBack == false
2. Creating the grid entirely in the code behind
this means that I should create the grid itself from codebehind in the OnInit phase

It's obvious that the second approach sucks compared to the the first one:
- The OnInit event is a VERY BAD place where to take decisions for programmatically creating columns in a grid. It is too early. Those decisions must quite always be taken in the onload phase of a page / control
- Creating a grid in code is always more painful than creating it directly in the aspx / ascx file. I have always to add a placeholder. Everytime I change something in the codebehind I've to recompile everything etc etc.

So I would use without doubt the second way of working, but continuing reading that article I reach this note:
"Column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState."
Applying naive Aristotle's logic I reach the conclusion that with template columns I've always to use abovementioned approach 2.

Sincerly at this point, I'd greatly welcome a tech explanation of the note about the template controls and the viewstate, because I can't figure out the problem behind it.
I mean, usually item templates are instantiated on each postback, so the instantiation itself should have nothing to do with the viewstate.
Unfortunately I've not bought yet the source code of your suite because I'm still evaluating it (I've an old web project that uses another third party suite of web controls that I can't stand anymore). Untill now I was quite happy with your controls, but RadGrid is bothering me a lot (I've also written a couple of posts about the inplace editing that is not good enough for me at the moment).

Best regards, Andrea Pirola

0
Iana Tsolova
Telerik team
answered on 13 Sep 2010, 03:05 PM
Hello pirobox,

Indeed, you are right that the help topic is a bit complex and this is because it covers all the possible approaches for programmatic creation of the grid.
However, when having GridTemplateColumns added dynamically, the grid should be created entirely on Page_Init as you already found. This limitation is due to the way RadGrid instantiates its controls by design and therefore cannot be changed.

Please excuse us for the inconveniece this might caused you.

Greetings,
Iana
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
Imran Javed Zia
Top achievements
Rank 1
answered on 17 Sep 2010, 01:49 PM
Hi,

We have simlar problem, In our scenario, we get list of columns in a RadList from user and then use clicks Load Button which post backs to same page and shows the data.

Now Problem is that List data is not avialable on page init nor in grid init. and we can only add them in Page Load or Button Click event. So we cannot add dynamic columns there. Now if we have enabled sorting or due to any reason page posts back the added template column are emptied as Columns are not added in Page_init 

Can you suggest some work around?

Thanks
0
Iana Tsolova
Telerik team
answered on 17 Sep 2010, 03:20 PM
Hello Imran,

The only workaround here is to create the grid on Page_Init. You can use some client-side code to add the selected items from the RadListBox (I assume by RadList you meant RadListBox) to a hidden field and on Page_Init get the hidden field value from the Form.Request object.

Greetings,
Iana
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
Imran Javed Zia
Top achievements
Rank 1
answered on 14 Oct 2010, 05:14 PM
Hi Iana,
I am sorry I was not able to check provided workaround but i have found that on Page_init Hidden Fields are not initialized too, But We can find through Request.Form as per information, but in this case we have to implement each and every related item that has some related options/impact.
Is there anyother solution to this?
Thanks
0
Iana Tsolova
Telerik team
answered on 15 Oct 2010, 01:42 PM
Hi Imran,

You can try requesting the RadListBox itself and see if you can get desired informaiton from there.

Greetings,
Iana
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
Imran Javed Zia
Top achievements
Rank 1
answered on 15 Oct 2010, 03:35 PM
Hi Iana,
I have already tried that, and there is only lstRadList_ClientState item and it is shows Items in Json format for first time only afterwords, it do not provide any thing,
We have other controls like DropDownList and I can access their selected value using following Code and it works fine by providing selected value
selVal = this.Request[ddl.UniqueID]   // as UniqueID returns control Name instead of ID

but in case of RadListBox this.Request[lst.UniqueID]   returns null and this.Request["lstRadList_ClientState"] returns Json data only for first time. So cannot be used in case of Paging and Sorting enabled

Thanks
0
Iana Tsolova
Telerik team
answered on 25 Oct 2010, 08:25 AM
Hello Imran,

In this case you can try creating the grid in a RadListBox server-side. However on the sebsequent postbacks, you need to recreate the grid on Page_Init.

Kind regards,
Iana
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
Tags
Grid
Asked by
pirobox
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
pirobox
Top achievements
Rank 1
Iana Tsolova
Telerik team
Imran Javed Zia
Top achievements
Rank 1
Share this question
or