How to find control on page by id
Is there a simple way to find a control in ASP.NET by id (in any nested container)? Other than traversing whole controls tree.
Something like this example:
TextBox tb = new TextB开发者_JAVA百科ox() { ID = "textboxId"};
panel3.Controls.Add(tb);
And in other method/class:
TextBox nameTextbox = MethodToFindControl("textboxId") as TextBox;
If I understand your question correctly, you could use method FindControl as follows:
TextBox nameTextbox = (TextBox) panel3.FindControl("textboxId") ;
No ... You have to traverse all controls tree until you find the control then you stop traversing, your method (MethodToFindControl) shall be a recursive method that takes two arguments: the root container (most of the time its the page) and the id of the control to look for.
In case you are not able to find the control using findcontrol declare corresponding td/panel as server control and then you should be able to easily find the required control within td or panel element.
DirectCast(tdMultiCnstrTypCode.FindControl("BBIMultiConsCode" & cnt + 1), DropDownList)
This approach is easier than traversing though all controls and child controls of the page.
精彩评论