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.
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.
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:
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?)"
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.
A couple more variations that may help you. They both use generics to return a specific type of control.
'Find first instance of a control:
Private Shared Function FindControlIterative(Of T As Control)(ByVal root As Control, ByVal id As String) As T
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
'Build collection of controls with matching ID (useful if root is a GridView control for example):
Private Shared Function FindControlCollection(Of T As Control)(ByVal root As Control, ByVal id As String) As List(Of T)
Dim ctl As Control = root
Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)
Dim ControlCollection As New List(Of T)
Do While (ctl IsNot Nothing)
If ctl.ID = id Then
ControlCollection.Add(ctl)
End If
For Each child As Control In ctl.Controls
If child.ID = id Then
ControlCollection.Add(child)
End If
If child.HasControls() Then
ctls.AddLast(child)
End If
Next
If (ctls.Count > 0) Then
ctl = ctls.First.Value
ctls.Remove(ctl)
Else
ctl = Nothing
End If
Loop
Return ControlCollection
End Function
bassplayer69
Member
6 Points
14 Posts
Re: Solution to the FindControl problem
Oct 16, 2007 12:21 PM|LINK
Here You go:
Make sure you add:
Imports System.Collections.Generic at the top of your code.
chalamarc
Member
88 Points
24 Posts
Re: Solution to the FindControl problem
Nov 10, 2007 02:02 PM|LINK
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.
david wendel...
All-Star
15865 Points
2243 Posts
Re: Solution to the FindControl problem
Nov 10, 2007 03:28 PM|LINK
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.
ks2007
Member
458 Points
301 Posts
Re: Solution to the FindControl problem
Nov 14, 2007 05:50 PM|LINK
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
Participant
853 Points
1290 Posts
Re: Solution to the FindControl problem
Nov 20, 2007 11:32 PM|LINK
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
Member
4 Points
4 Posts
Re: Solution to the FindControl problem
Dec 21, 2007 03:41 PM|LINK
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
Participant
853 Points
1290 Posts
Re: Solution to the FindControl problem
Dec 21, 2007 09:40 PM|LINK
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.
ppp
Member
2 Points
1 Post
Re: Solution to the FindControl problem
Jan 16, 2008 08:39 AM|LINK
good job
IRumsey
Member
2 Points
1 Post
Re: Solution to the FindControl problem
Feb 26, 2008 05:41 PM|LINK
A couple more variations that may help you. They both use generics to return a specific type of control.
'Find first instance of a control: Private Shared Function FindControlIterative(Of T As Control)(ByVal root As Control, ByVal id As String) As T 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 'Build collection of controls with matching ID (useful if root is a GridView control for example): Private Shared Function FindControlCollection(Of T As Control)(ByVal root As Control, ByVal id As String) As List(Of T) Dim ctl As Control = root Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control) Dim ControlCollection As New List(Of T) Do While (ctl IsNot Nothing) If ctl.ID = id Then ControlCollection.Add(ctl) End If For Each child As Control In ctl.Controls If child.ID = id Then ControlCollection.Add(child) End If If child.HasControls() Then ctls.AddLast(child) End If Next If (ctls.Count > 0) Then ctl = ctls.First.Value ctls.Remove(ctl) Else ctl = Nothing End If Loop Return ControlCollection End Functionks2007
Member
458 Points
301 Posts
Re: Solution to the FindControl problem
Feb 27, 2008 12:44 AM|LINK
Could you please provide a C# version of the code since I am not familiar with VB?
Thanks.