Search code examples
asp.netcontrolsfindcontrol

findcontrol does not find dynamically added control which was just addes one line before


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?


Solution

  • 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