problem iterating through controls on a web form.

  • Thread starter Thread starter Milsnips
  • Start date Start date
M

Milsnips

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.
 
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

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.
 

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

Back
Top