Columns dynamic creation

10 posts, 0 answers
  1. pirobox
    pirobox avatar
    16 posts
    Member since:
    Nov 2009

    Posted 07 Sep 2010 Link to this post

    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

  2. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 08 Sep 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. pirobox
    pirobox avatar
    16 posts
    Member since:
    Nov 2009

    Posted 08 Sep 2010 Link to this post

    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

  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 13 Sep 2010 Link to this post

    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
  6. Imran Javed Zia
    Imran Javed  Zia avatar
    55 posts
    Member since:
    Jul 2009

    Posted 17 Sep 2010 Link to this post

    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
  7. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 17 Sep 2010 Link to this post

    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
  8. Imran Javed Zia
    Imran Javed  Zia avatar
    55 posts
    Member since:
    Jul 2009

    Posted 14 Oct 2010 Link to this post

    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
  9. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 15 Oct 2010 Link to this post

    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
  10. Imran Javed Zia
    Imran Javed  Zia avatar
    55 posts
    Member since:
    Jul 2009

    Posted 15 Oct 2010 Link to this post

    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
  11. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 25 Oct 2010 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017