Dan Scott
Dan Scott

Reputation: 137

Using FindControl on Dynamically Generated Controls

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?

Upvotes: 1

Views: 2025

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189457

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.

Upvotes: 1

Related Questions