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

Help with Dynamic Controls in ItemDatabound

3 Answers 249 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 04 Apr 2013, 02:21 AM
I am trying to retrieve the values of dynamically created controls in a Radgrid.  I have read dozens of posts similar to this, but cannot seem to sort it out.

Here is the scenario:

  1. I have a Radgrid on my page with static GridTemplateColumns setup
  2. In one column I have a blank ItemTemplate
  3. OnItemDatabound (which happens as an AJAX callback) I insert a dynamic control, and potentially set it's value (e.g. textbox, datepicker, combobox).  This control type is based on the data - so I cannot do this in OnItemCreated
  4. Everything looks right when the screen comes back - I have a grid with custom controls in each row, and the proper values all set
  5. Then you do some other things on the screen - and eventually click SAVE
  6. On Save I loop through the Radgrid items, find the controls by ID and retrieve their values - to then save off

It is the last part that has me stumped.

At first I was getting a null reference to the controls - and found that my dynamic controls where not in the GridItem container where I had created them.

After a bit of reading, I added code that loops through and re-initializes the controls in the grid.  To do this I Rebind the Radgrid right before looping through the Radgrid items, setting a flag that has the code in ItemDatabound only create the Controls with the proper ID and then .Adds() them to the GridDataItem - skipping the parts that set the default values of the controls

At this point I can now get the control with .FindControl(myID) - but the .text or .selectedvalue, etc is null.  

Is this kind of scenario supported with the RadGrid and AjaxManager?

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Apr 2013, 12:21 PM
0
Dan
Top achievements
Rank 1
answered on 04 Apr 2013, 03:45 PM
Thanks.

It looks like even if you are not binding data to the Grid - you still need to controls to exist in the Template.  The easiest way for me was just to add the 4 different controls I might use to the ItemTemplate, set the to Visible=False, then just turn the one on that I need.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Apr 2013, 11:28 AM
Hello,

Yes it is better to show/hide column based on conditions.

Please use Visible property for this.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Share this question
or