CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.faq_frequently_asked_questions Tags:
Item Type: NewsGroup Date Entered: 5/5/2007 11:12:47 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 24 Views: 83 Favorited: 0 Favorite
25 Items, 2 Pages 1 2 |< << Go >> >|
justSomeone
Asp.Net User
Solution to the FindControl problem5/5/2007 11:12:47 AM

0

I have seen may posts about having problems with the FindControl method. Most seem to come about because the control being searched for is nested within a container other than the webform.

I came across this code (sorry dont remember the web site)  that I have posted in response to many of the posts related to this type of problem. I thought it would be easier if I post this code here for others to find.

/// <summary>

/// Finds a Control recursively. Note finds the first match that exists

/// </summary>

/// <param name="ContainerCtl">Should be the lowest container in the heirarchy, for eg dont choose Master page if you can pick the specific panel</param>

/// <param name="IdToFind">ID of the control you are looking for</param>

/// <returns>the control if found else null</returns>

private static Control FindControlRecursive(Control Root, string Id)

{

if (Root.ID == Id)

return Root;

foreach (Control Ctl in Root.Controls)

{

Control FoundCtl = FindControlRecursive(Ctl, Id);

if (FoundCtl != null)

return FoundCtl;

}

return null;

}

kimoy
Asp.Net User
Re: Solution to the FindControl problem5/5/2007 12:47:37 PM

0

funny, i just posted the same code minutes ago to somebody in a different thread.
kaushalparik27
Asp.Net User
Re: Solution to the FindControl problem5/7/2007 4:37:57 AM

0

hi friend...

thanks for the snippet... would be help ful... 


Thanx,
[KaushaL] | Profile | Blog
Dont forget to click ?Mark as Answer? on the post that helped you.
This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.



"I would love to change the world, but they won?t give me the source code"
CiscoCylk
Asp.Net User
Re: Solution to the FindControl problem6/15/2007 6:42:33 PM

0

I don't really understand this explanation; it is rather cryptic and I understand C# a little, but I wish it were in VB.

 

Are you saying that first we need to find the webform, then formview, and then the button in my case?  Why do I need todo that, andyway it doesn't work.

 

I I tried writing this in VB but was stumped as to how you call FindControlRecursive from within itself?

 

justSomeone
Asp.Net User
Re: Solution to the FindControl problem6/16/2007 6:01:15 AM

0

CiscoCylk I have dug up the post that I originally found the code at

http://www.west-wind.com/WebLog/posts/5127.aspx he provides some info on why the problem occurs.

Basically I first hit this problem when I started using some of the new Login control provided with ASP.NET 2.0. It seems like they inlcude an additional container that hides the controls within so that doing a FindControl at the page level doesnt pick them up.

The code does work for me, on a number of occasions and a number of different applications, so if you would care to post up how you are trying to use the code I could offer an opinion.

Not sure about the 3rd part of your question, as to how I call FindControlRecursive from within itself. Do you mean that you dont understand recursion or is there something else you dont understand

stelianx
Asp.Net User
Re: Solution to the FindControl problem6/20/2007 8:50:48 AM

0

This is true, solution may be your or that:

http://www.codinghorror.com/blog/archives/000307.html

 But recursion is expensive, so better will be:

private static Control FindControlIterative(Control root, string id)

{

Control ctl = root;

LinkedList<Control> ctls = new LinkedList<Control>();

while (ctl != null)

{

if (ctl.ID == id)return ctl;

 

foreach (Control child in ctl.Controls)

{

if (child.ID == id)

return child;if (child.Controls.Count > 0)

ctls.AddLast(child);

}

if (ctls.Count > 0)

{

ctl = ctls.First.Value;

ctls.Remove(ctl);

}

else

ctl = null;

}

return null;

}


Yours,
Popa D. Stelian
MCP/MCAD/MCSD
ROMANIA
stelianx
Asp.Net User
Re: Solution to the FindControl problem6/20/2007 9:03:34 AM

0

Better version is bellow

private static Control FindControlIterative(Control root, string id)
        {
            Control ctl = root;
            LinkedList ctls = new LinkedList();

            while (ctl != null)
            {
                if (ctl.ID == id)
                    return ctl;
                foreach (Control child in ctl.Controls)
                {
                    if (child.ID == id)
                        return child;
                    if (child.HasControls())
                        ctls.AddLast(child);
                }
                ctl = ctls.First.Value;
                ctls.Remove(ctl);
            }
            return null;
        }
 
Yours,
Popa D. Stelian
MCP/MCAD/MCSD
ROMANIA
stelianx
Asp.Net User
Re: Solution to the FindControl problem6/21/2007 9:23:02 AM

0

 

private static Control FindControlIterative(Control root, string id)
        {
            Control ctl = root;
            LinkedList ctls = new LinkedList();

            while (ctl != null)
            {
                if (ctl.ID == id)
                    return ctl;
                foreach (Control child in ctl.Controls)
                {
                    if (child.ID == id)
                        return child;
                    if (child.HasControls())
                        ctls.AddLast(child);
                }
                ctl = ctls.First.Value;
                ctls.Remove(ctl);
            }
            return null;
        }
 
Yours,
Popa D. Stelian
MCP/MCAD/MCSD
ROMANIA
Rodashar
Asp.Net User
Re: Solution to the FindControl problem9/20/2007 4:57:21 PM

0

Question?? Don't most of the controls you can nest other controls in have a FindControl method?? If you already have the id of the control you want and I assume an idea of the id for the controls container why could you not use the built in methods to search for this control??


Remeber to mark as Answer if someone's answer helped you.
dhassen
Asp.Net User
Re: Solution to the FindControl problem10/3/2007 12:51:56 PM

0

 I don't understand how you use the code you posted - where do you type that? Also, can someone translate to VB please?

tysonh28
Asp.Net User
Re: Solution to the FindControl problem10/5/2007 10:51:24 PM

0

There's some irony to be noted here. If you read Scottgu's comment at the top, he says that they debated sticking in a recursive FindControl(), but decided against it because of the potential for newbies to start abusing it and thereby negatively affecting their page's performance. What's ironic is that people went ahead and did what MS didn't want -- wrote their own recursive methods to find controls!

Either way we get the shaft. Use recursion to find a control and you're going to pay for it in terms of performance. Use FindControl("ctl00").FindControl("whatever").FindControl("whatever2") and you won't pay the performance price, but it could come back to haunt you in the future should you add even one more layer (i.e. - panel, user control, etc.) inbetween.


This is the part where I politely request you to mark my response as the Answer, under the pretense that doing so will greatly help the community, blah, blah...
When in actuality I'm singlely intent upon the wopping 10 points that I'll receive!
... Oops, was that my outloud voice? ;)
bassplayer69
Asp.Net User
Re: Solution to the FindControl problem10/16/2007 12:21:08 PM

0

dhassen:

 I don't understand how you use the code you posted - where do you type that? Also, can someone translate to VB please?

 

 

Here You go:

 

	Private Shared Function FindControlIterative(ByVal root As Control, _
		ByVal id As String) As Control

		Dim ctl As Control = root
		Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)

		Do While (ctl IsNot Nothing)
			If ctl.ID = id Then
				Return ctl
			End If
			For Each child As Control In ctl.Controls
				If child.ID = id Then
					Return child
				End If
				If child.HasControls() Then
					ctls.AddLast(child)
				End If
			Next
			ctl = ctls.First.Value
			ctls.Remove(ctl)
		Loop

		Return Nothing

	End Function

  Make sure you add:

 

Imports System.Collections.Generic at the top of your code.

 

 

chalamarc
Asp.Net User
Re: Solution to the FindControl problem11/10/2007 3:02:59 PM

0

i think using of findcontrol() on your formview or anyother control name, it is the best way as you neednot to actually go down on your performance and also you can get rid of all the code that has been shown on the top and one which make full use of the api's and its functions which microsoft provides.


C.C.Reddy
david wendelken
Asp.Net User
Re: Solution to the FindControl problem11/10/2007 4:28:06 PM

0

chalamarc:

i think using of findcontrol() on your formview or anyother control name, it is the best way as you neednot to actually go down on your performance and also you can get rid of all the code that has been shown on the top and one which make full use of the api's and its functions which microsoft provides.

 

If that were a valid argument, we would still be coding in COBOL and FORTRAN, or maybe 1s and 0s. :) 

Computer Science is the science of giving away something you want to get something else you want more.

Which route to use is simply a matter of priorities.  The recursive (or iterative) versions of FindControl provide the following benefits and drawbacks:

-  Runtime Performance
+ Codetime Performance (it's faster and more convenient for the programmer!)
+ Robustness when the number of levels in the UI Layer changes
+ Easier code to read.

Hard-wiring the reference by nesting chaining FindControl methods offers the following benefits and drawbacks:

+ Runtime Performance
-  Codetime Performance
-  Robustness
-  Harder to read.

Pick the method that best meets your needs.

 


If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
ks2007
Asp.Net User
Re: Solution to the FindControl problem11/14/2007 6:50:24 PM

0

I am new to ASP.net and I would like to know how you would actually use the above method. Could you please give an example?

Thanks.

muybn
Asp.Net User
Re: Solution to the FindControl problem11/21/2007 12:32:09 AM

0

I receive this error message while trying to run this procedure: "The type or namespace name 'Generic' does not exist in the class or namespace 'System.Collections' (are you missing an assembly reference?)"

 

art03
Asp.Net User
Re: Solution to the FindControl problem12/21/2007 4:41:09 PM

0

I get a null reference exception for this line:

 ctl = ctls.First.Value;

 When I try to use that code. Any ideas why? Thanks.
 

muybn
Asp.Net User
Re: Solution to the FindControl problem12/21/2007 10:40:14 PM

0

I've been through something similar a few times myself so I can probably help you. Before I try that, however, please give me the following information so I can customize an answer to your question:

(1) VB or C#?   (2) The code for your control and what you want to find.

pradipd.mst
Asp.Net User
Re: Solution to the FindControl problem1/9/2008 12:33:40 PM

0

Nice Answer For the Find Control Problem

ppp
Asp.Net User
Re: Solution to the FindControl problem1/16/2008 9:39:19 AM

0

good job

 

 

25 Items, 2 Pages 1 2 |< << Go >> >|


Free Download:




   
  Privacy | Contact Us
All Times Are GMT