Reputation: 881
who can explain this to me?
CheckBox ckRequest = new CheckBox();
ckRequest.ID = "ckRequest";
ckRequest.DataBinding += new EventHandler(this.CkIsRequested_DataBinding);
container.Controls.Add(ckRequest);
Control con = container.FindControl("ckRequest");
Debugging shows that con is still null.
Debugging also shows me, that conteiner.Controls hase one Item with ID "ckRequest"
How can this be????
Many thanks for your answers.
Actually I try the following. findcontrol does not find dynamically created control in rowUpdating eventhandler It makes sense to me, that findcontrol works only on the created page.
At which point in time the visual tree of the page is created?
Upvotes: 5
Views: 6438
Reputation: 716
You might want to try to following:
//GET THE CHECKBOXLIST
Control c = phCategories.Controls.Cast<Control>().First(item => item.ID == "cblCatID-" + catID && item.GetType().Name == "CheckBoxList");
if (c.GetType().Name == "CheckBoxList")
{
cbl = (CheckBoxList)c;
}
For some reason I needed to cast this as a control first. If I did not do it this way, I seemed to grab a label instead (which didn't make sense to me, because it wasn't actually grabbing a label). Hope it helps somebody.
Upvotes: 0
Reputation: 14781
You can use the following:
Control con =
container.Controls.Cast<Control>().First(item => item.ID == "ckRequest");
Upvotes: 2
Reputation: 26727
FindControl
only works when the control is in the visual tree of the page
In your case you can try this
var checkBoxesInContainer = container.Controls.OfType<CheckBox>();
http://msdn.microsoft.com/en-us/library/bb360913.aspx
Upvotes: 3