I am building a DataTable dynamically at run time and I am also using templates in the Cells that again are dynamically loaded.
The template is currently extremely simple in that it consists solely a CheckBos and a HiddenField like so:
I trap the ItemDataBound Event of the Grid and have code similar to that below:
My question is why do I have to have the Controls[0]. before the FindControl() calls? I don't like hard coding things like that and would prefer a more elegant way to find the control in the template.
It is wo0rking now, but it's inelegant and it took me a long time to figure it out and I am sure it will get broken b y a future release.
Martin
The template is currently extremely simple in that it consists solely a CheckBos and a HiddenField like so:
<
asp:CheckBox
ID
=
"cb"
runat
=
"server"
/> <
asp:HiddenField
ID
=
"hf"
runat
=
"server"
/>
I trap the ItemDataBound Event of the Grid and have code similar to that below:
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
DataRowView drv = (DataRowView)item.DataItem;
for
(
int
i = 0; i < rgTags.MasterTableView.Columns.Count; i++ )
{
string
uniqueName = rgTags.MasterTableView.Columns[i].UniqueName;
if
(drv[i]
is
System.DBNull)
{
item[uniqueName].Controls[0].FindControl(
"cb"
).Visible =
false
;
}
else
{
AdminTag tag = (AdminTag)drv[i];
((CheckBox)item[uniqueName].Controls[0].FindControl(
"cb"
)).Text = tag.sText;
((HiddenField)item[uniqueName].Controls[0].FindControl(
"hf"
)).Value = tag.nTagId.ToString();
}
}
}
My question is why do I have to have the Controls[0]. before the FindControl() calls? I don't like hard coding things like that and would prefer a more elegant way to find the control in the template.
It is wo0rking now, but it's inelegant and it took me a long time to figure it out and I am sure it will get broken b y a future release.
Martin