views:

688

answers:

1

I have some ASP.NET code that dynamically generates rows and cells for a Table control on the page.

I have specifically set the ID of each cell, but I am having trouble getting FindControl to actually find them.

Here's the code that I use to create the cells:

tbc = New TableCell
tbr.Cells.Add(tbc)
tbc.ID = String.Format("tc_{0}-{1}-{2}", curStartDate.Day, curStartDate.Month, curStartDate.Year)

Just below that, I try to find the control with the following:

Dim ctlName As String = String.Format("tc_{0}-{1}-{2}", curStartDate.Day, curStartDate.Month, curStartDate.Year)
Dim ctl As Control = tblAllocations.FindControl(ctlName)

I have tried swapping the line that declares the ID, with the line that adds the cell to the Cells collection of the TableRow, and that makes it work. But throughout my application, I have the statements in the order as above, and they works fine (FindControl can find the control with the correct ID).

Is there something obvious I am missing?

+1  A: 

When you say "just below that" have you added the tbr to the tblAllocations.Rows yet? If not that would be the reason it can't be found.

AnthonyWJones
Yes, it has definitely been added. I can see the row and the cells in the output of the page. The cells have their ID outputted to the HTML as well.
Dan Scott
I meant, at the point when you call FindControl has the row been added to the table? tblAllocations.Rows.Add(tbr) needs to have happened before tblAllocations.FindControl will find a cell in that tbr
AnthonyWJones
It has definitely been added correctly. I think I have narrowed down the problem though. I have DropDownList that I call the DataBind method on before I create the controls. When I remove that call to DataBind, everything works correctly. It's really weird.
Dan Scott