By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

problem iterating through controls on a web form.

P: n/a
hi there,

i have a strange problem. I want to programatically loop through each
control on a page, but am having issues.

my test example:

1. header.ascx - the ascx control finds all the child controls no problems

2. default.aspx - i dont find any controls that are placed directly on the
form.

i call the function as follows

AddControlsToDatabase(Me) - this is placed on every page_ for both ASPX
pages and ASCX pages.
my function is as follows:
Sub AddControlsToDatabase(ByVal mypage As Object)
For Each c As Control In mypage.controls
'hyperlink
If TypeOf c Is HyperLink Then
Dim l As HyperLink = CType(c, HyperLink)
'...my code goes here...
End If
'Button
If TypeOf c Is Button Then
Dim l As Button = CType(c, Button)
'...my code goes here...
End If
Next
End Sub

Any help is appreciated in why this doesnt pickup the ASPX page controls
within the Controls collection.

thanks,

Paul.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
For Each Control In Me.Controls

Next
"Milsnips" <mi******@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
hi there,

i have a strange problem. I want to programatically loop through each
control on a page, but am having issues.

my test example:

1. header.ascx - the ascx control finds all the child controls no
problems

2. default.aspx - i dont find any controls that are placed directly on
the
form.

i call the function as follows

AddControlsToDatabase(Me) - this is placed on every page_ for both ASPX
pages and ASCX pages.
my function is as follows:
Sub AddControlsToDatabase(ByVal mypage As Object)
For Each c As Control In mypage.controls
'hyperlink
If TypeOf c Is HyperLink Then
Dim l As HyperLink = CType(c, HyperLink)
'...my code goes here...
End If
'Button
If TypeOf c Is Button Then
Dim l As Button = CType(c, Button)
'...my code goes here...
End If
Next
End Sub

Any help is appreciated in why this doesnt pickup the ASPX page controls
within the Controls collection.

thanks,

Paul.

Nov 18 '05 #2

P: n/a
thanks.. problem solved.

"Scott M." <s-***@nospam.nospam> wrote in message
news:OE*************@TK2MSFTNGP10.phx.gbl...
For Each Control In Me.Controls

Next
"Milsnips" <mi******@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
hi there,

i have a strange problem. I want to programatically loop through each
control on a page, but am having issues.

my test example:

1. header.ascx - the ascx control finds all the child controls no
problems

2. default.aspx - i dont find any controls that are placed directly on
the
form.

i call the function as follows

AddControlsToDatabase(Me) - this is placed on every page_ for both ASPX
pages and ASCX pages.
my function is as follows:
Sub AddControlsToDatabase(ByVal mypage As Object)
For Each c As Control In mypage.controls
'hyperlink
If TypeOf c Is HyperLink Then
Dim l As HyperLink = CType(c, HyperLink)
'...my code goes here...
End If
'Button
If TypeOf c Is Button Then
Dim l As Button = CType(c, Button)
'...my code goes here...
End If
Next
End Sub

Any help is appreciated in why this doesnt pickup the ASPX page controls
within the Controls collection.

thanks,

Paul.


Nov 18 '05 #3

P: n/a
For .aspx pages, the controls are placed inside the form (<form> tag), not
at the page level itself. So, you have to loop through the Controls
collection of the form control instance.

Dim f As HtmlForm = Me.FindControl (<form tag id>)
For Each c As Control in f.Controls
....
If TypeOf c Is Button Then
End If
....
Next

As discussed earlier in the group, you may have to use recursion to
loop-through controls inside a container control which could be a member of
a Controls collection.

HTH

"Milsnips" <mi******@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
hi there,

i have a strange problem. I want to programatically loop through each
control on a page, but am having issues.

my test example:

1. header.ascx - the ascx control finds all the child controls no problems

2. default.aspx - i dont find any controls that are placed directly on the
form.

i call the function as follows

AddControlsToDatabase(Me) - this is placed on every page_ for both ASPX
pages and ASCX pages.
my function is as follows:
Sub AddControlsToDatabase(ByVal mypage As Object)
For Each c As Control In mypage.controls
'hyperlink
If TypeOf c Is HyperLink Then
Dim l As HyperLink = CType(c, HyperLink)
'...my code goes here...
End If
'Button
If TypeOf c Is Button Then
Dim l As Button = CType(c, Button)
'...my code goes here...
End If
Next
End Sub

Any help is appreciated in why this doesnt pickup the ASPX page controls
within the Controls collection.

thanks,

Paul.

Nov 18 '05 #4

P: n/a
Your welcome...Good luck!

"Milsnips" <mi******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
thanks.. problem solved.

"Scott M." <s-***@nospam.nospam> wrote in message
news:OE*************@TK2MSFTNGP10.phx.gbl...
For Each Control In Me.Controls

Next
"Milsnips" <mi******@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
> hi there,
>
> i have a strange problem. I want to programatically loop through each
> control on a page, but am having issues.
>
> my test example:
>
> 1. header.ascx - the ascx control finds all the child controls no
> problems
>
> 2. default.aspx - i dont find any controls that are placed directly
> on
> the
> form.
>
> i call the function as follows
>
> AddControlsToDatabase(Me) - this is placed on every page_ for both
> ASPX
> pages and ASCX pages.
>
>
> my function is as follows:
> Sub AddControlsToDatabase(ByVal mypage As Object)
> For Each c As Control In mypage.controls
> 'hyperlink
> If TypeOf c Is HyperLink Then
> Dim l As HyperLink = CType(c, HyperLink)
> '...my code goes here...
> End If
>
>
> 'Button
> If TypeOf c Is Button Then
> Dim l As Button = CType(c, Button)
> '...my code goes here...
> End If
> Next
> End Sub
>
>
>
> Any help is appreciated in why this doesnt pickup the ASPX page
> controls
> within the Controls collection.
>
> thanks,
>
> Paul.
>
>



Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.