Last post Apr 20, 2011 09:13 PM by atconway
Apr 18, 2011 08:48 PM|geo2004|LINK
on the page. Speed is also an important factor, so I don't want to do a recursive lookup unless I have to (I will be doing this for up to 100 controls on the page, and the page could be accessed by about 100 people at a time).
I was hoping I could do something with Reflection, similar to accessing a class Property by it's name, but I haven't had any luck. For Example: MyClass.GetType().Getproeprty(PropertyName).....
Apr 19, 2011 12:33 AM|umairaslam22|LINK
ControlName.ClientID you can get it ID
Apr 19, 2011 01:14 PM|geo2004|LINK
Apr 19, 2011 01:18 PM|LudovicoVan|LINK
> FindControl() isn't exactly what I am looking for because the control could be anywhere on the page. Speed is also an important factor, so I don't want to do a recursive lookup unless I have to (I will be doing this for up to 100 controls on the page,
and the page could be accessed by about 100 people at a time).
Hmm, I don't think there is anything intrinsically more efficient than a FindControl (assuming your controls are dynamically generated). And I cannot see any way to optimize the process or avoid a recursive lookup, as long as the control could be anywhere
on the page. Maybe you will have to rethink/rationalize your page layout a little bit to get the best results.
Apr 19, 2011 02:19 PM|abhisheks|LINK
You could use overloaded findcontrol(string,offset) rather than using findcontrol(id) directly. With the overloaded method, you have bettere chance of success in terms of speed.
Recursion would be next option, but you already mentioned that you don't want to do it.
Apr 19, 2011 04:05 PM|geo2004|LINK
I was working with my manager on this issue, and we were playing around with reflection, specifically GetMembers and GetFields. Neither of these conained any references to the controls on the page. Anyone have any idea if there's a Get...() function that
returns a list or specific control on a page?
It seems like it would be possible because Intellisense is able to generate that list, unless Intellisense doesn't use reflection to generate it's list of members/fields.
Apr 19, 2011 11:32 PM|atconway|LINK
I was hoping I could do something with Reflection
If you do come to a solution using the System.Reflection namespace, you might want to use a performance analyzer tool, performance monitors, etc to place it vs. recursion. Reflection inherently has an overhead to it, so it is funny when you talk about
wanting performance, but yet want to use reflection as the solution. You might be surprised at how efficient recursion can be if written properly. I have a treeview in 1 app that has some monstrous number of items in it which uses recursion in .NET
code to manipulate it, and it is blindingly fast. So I wouldn't count out recursion without placing some metrics around it first.
Honestly I have not decompiled the System.Reflection .dll recently, but it wouldn’t surprise me if there was some recursion going on under the covers at times when trying to find methods or properties by string value. Not sure on this, but just stating
that Reflection might not perform any better.
Another option to investigate would be the old "divide and conquer" approach doing the task asynchronously. Depending on your environment (multi-core processor), and if you have threads available, you might be able to come up with an async approach
that is fastest.
However I recommend going the easy route (FindControl(), recursion, etc.) and prove its slow or under performing with some real metrics before investing a lot of time in an alternative solution that might not gain you much in the end anyways.
Apr 20, 2011 09:43 AM|LudovicoVan|LINK
> Anyone have any idea if there's a Get...() function that returns a list or specific control on a page?
The Control class and derived expose a Controls property that returns a ControlCollection instance.
That said, I agree on what already noticed: the FindControl route looks the best in terms both of readability and performance. Besides, as a general rule, never optimize unless against specific bottlenecks and with the support of some proper benchmarking.
Apr 20, 2011 03:35 PM|geo2004|LINK
Well I decided to give a recursive function a try, and have to admit I'm surprised at how fast it actually is. All of the editing on my page takes place within a tab container, so I'm able to pass that as the root control to begin the search and limit the
depth of the search. So far I'm getting results in <1ms. I still have a lot more controls to add to the page, but so far I think this solution will work.
Thanks for the info everyone
The function is taken from one I found online, here is the code if anyone else is interested:
private Control FindControlById(Control c, string id)
if (c.ID == id)
foreach (Control control in c.Controls)
Control ctlToFind = FindControlById(control, id);
if (ctlToFind != null)
Apr 20, 2011 09:13 PM|atconway|LINK
Well I decided to give a recursive function a try, and have to admit I'm surprised at how fast it actually is.
Good point to reiterate: code also should be "Innocent until proven guilty" ... or something to that effect. Don't just assume something will be slow, until metrics are placed around the suspect code to see if it really is the issue. Glad you found a solution,
and thank you for posting the code; it will help future readers.