Getting values from dynamically created controls in a DataGrid

Getting values from dynamically created controls in a DataGrid

Post by Colin McGu » Sat, 07 Feb 2004 09:48:15


This is a translation of an ASP page to ASP.Net. First, the background:

The goal is to have a grid of different settings for the application --
think something along the lines of the Property Page for a control. You
have two columns -- the first column is a simple caption, and the second
column can be one of a number of controls (textbox, checkbox,
dropdownlist, etc) for each row. So row #1 might have a textbox in the
second column, row #2 might have a checkbox, etc.

In ASP, this was done just by writing out a <table> with two <td> cells
per <tr> row; in the second <td> cell, an <input type="text">, <input
type="checkbox">, or <select> would be written out, as appropriate.
Some translations had to be done when creating the page (the settings
that were dropdown lists would be numerical values, so "2" would
indicate that the third <option> in the <select> should be selected; it
would have to translate "0" and "1" to be either "" or
"checked=\"checked\"" for a checkbox, that sort of thing.)

But now it's ASP.Net. Most of the translation has been pretty easy, and
a lot of ugly HTML generation has been removed in favor of the objects,
which is great. But this part is giving me trouble.

All of the previous tables are now DataGrids, so it was decided that
this should be a DataGrid too, for consistency. A TemplateColumn was
added to the DataGrid. In the DataGrid's ItemDataBound event, the
proper HtmlElement (HtmlInputText, HtmlInputCheckBox, HtmlSelect, etc)
is created, and added to the appropriate cell.

This all works fine. Looks great.

However, saving didn't work at all. Checking the source of the code, I
found that when an item was assigned an id of "LogoFile", it's actual id
on the page would be set to "gridsettings__ctl2_LogoFile", and it's name
(which was assigned as "LogoFile") became "gridpreference:_ctl2:LogoFile".

So, the Request.Form was looking for names like "LogoFile" that weren't
actually coming through, and since these items are created dynamically,
posting back doesn't work for them either (they only get their correct
ids, it seems after the page has been rendered)

So my options seem to boil down to:

#1: Subclass the HtmlElement objects and override their methods.
Doesn't strike me as elegant, nor a good idea.

#2: Somehow delay the saving of items on a PostBack until after the
rendering is done. Don't even know if this is possible, or a good idea.

#3: Dump the DataGrid, use a table styled like a DataGrid. This seems
the easiest of the solutions.

#4: Some other method I'm not aware of?

Thoughts?

--Colin McGuigan
 
 
 

Getting values from dynamically created controls in a DataGrid

Post by Marin » Sat, 07 Feb 2004 11:15:13

You are using objects for the rendering, yet still looking in Request.Form
for the data afterwards?

You should be examining the grid and its controls, finding the control you
need in each row, and getting its data that way. Remember, the datagrid is
an object, that contains other objects.

 
 
 

Getting values from dynamically created controls in a DataGrid

Post by Colin McGu » Sat, 07 Feb 2004 21:03:27


But the objects don't exist in the DataGrid until I create them, which,
as I understand the lifecycle, is sometime after the PostBack data has
been inserted into the objects.

Am I wrong?

--Colin McGuigan