Get Reference to a Specific Control in the Controls Collection

J

Jeffrey Todd

I need to process a specific Control in a Web Page's Controls collection.

I do know the ID of the control.

I simply need to pass a reference to it to another method.

The following code works - but I suspect it's overly complicated for what
I'm trying to do. So, what is a simpler way to get the reference to the
Control that has the ID "MyControl" - simpler than looping through the
Controls collection?

foreach(System.Web.UI.Control c in Controls) {
if (c.ID == "MyControl") {
DoSomething(c);
break;
}
}

Thanks!
 
D

David Lloyd

Jeffrey:

One alternative is to use the FindControl method of the Page class which
takes an ID property as a parameter.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I need to process a specific Control in a Web Page's Controls collection.

I do know the ID of the control.

I simply need to pass a reference to it to another method.

The following code works - but I suspect it's overly complicated for what
I'm trying to do. So, what is a simpler way to get the reference to the
Control that has the ID "MyControl" - simpler than looping through the
Controls collection?

foreach(System.Web.UI.Control c in Controls) {
if (c.ID == "MyControl") {
DoSomething(c);
break;
}
}

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top