StackOverFlowException When Recursing Page Controls

R

Randy

I'm trying to recurse the controls on a web page and insert some data
about the controls into a database but I get a StackOverflowException
before a counter I set for debugging purposes even increments one time.
Here's the button code:

Protected Sub btnGetCtrls_Click(ByVal sender As Object, ByVal e As
EventArgs) _
Handles btnGetCtrls.Click
Dim strFormID As String
ControlAdapter.Fill(ControlTable) 'Probably not necessary
strFormID = Page.Form.ID 'Gets the ASP form ID to use for each record
GetControlInfoRecurse(Page, strFormID)
End Sub

Here's the function code:

Public Sub GetControlInfoRecurse(ByVal oControl As
System.Web.UI.Control, _
ByVal sFrmID As String)
Dim oChildControl As Control
Dim sType As String
Dim strSEM As String
Dim l As Long 'For debugging

'We are only interested in certain control types
'so test for those
sType = oControl.GetType().ToString()
If sType = "checkbox" Or sType = "select-one" _
Or sType = "radio" Or sType = "textbox" Or sType = "text" _
Then
Try
ControlAdapter.InsertQuery(oControl.ID, sFrmID, _
oControl.GetType().ToString())
Catch ex As Exception
strSEM = ex.Message
End Try
End If
l += l

For Each oChildControl In oControl.Controls
GetControlInfoRecurse(oControl, sFrmID)
Next

End Sub

Eventually, I want to turn this into a user control so I can record the
relevant controls on each of several web pages in an application.
The logic seems straight forward enough: From the button, I initially
pass the Page to the recursive function. First things first, I ought to
get a DB record of the Page itself. I know this part works because I
tested it before I put the recursive part in. When I put the recursive
part in, however, I don't even get the Page DB record before the
StackOverflowException happens. This seems odd -- is VS anticipating
that there might be a stack overflow. In any event, the recursion seems
very straight forward. For each control in the Page.Controls
collection, call the recursion again. Where is this unbounded?
Heeellllpppp!
Thanks.
Randy
 
R

Randy

Solved the main issues here. First, had to put the recursive part in a
test for whether a control has any child controls:

If oControl.HasCongtrol then
'Recurse here
End If

But, that wasn't all. I'm at home now so I'll have to post proper
credit tomorrow when I find the article I got the information from. I
rewrote the button click to pass a ControlCollection to the sub rather
than a control, and rewrote the sub to handle it that way. If anybody's
interested, I'll post the rewritten code tomorrow along with the
proper attribution.
Thanks.
Randy
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top