Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 353
Ephedra Avatar asked Feb 14 '13 10:02

Ephedra


2 Answers

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

like image 105
Massimiliano Peluso Avatar answered Oct 21 '22 19:10

Massimiliano Peluso


You can use the following:

Control con = 
    container.Controls.Cast<Control>().First(item => item.ID == "ckRequest");
like image 30
Akram Shahda Avatar answered Oct 21 '22 21:10

Akram Shahda