StackOverFlowException When Recursing Page Controls

Discussion in 'ASP .Net Web Controls' started by Randy, Jan 18, 2006.

  1. Randy

    Randy Guest

    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
     
    Randy, Jan 18, 2006
    #1
    1. Advertising

  2. Randy

    Randy Guest

    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
     
    Randy, Jan 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars-Erik Aabech
    Replies:
    8
    Views:
    758
    Steven Cheng[MSFT]
    Apr 29, 2005
  2. snowdy

    Recursing code problem

    snowdy, Aug 29, 2003, in forum: C Programming
    Replies:
    19
    Views:
    1,179
    Kevin D. Quitt
    Sep 2, 2003
  3. Scott Carlson

    recursing through files in a folder

    Scott Carlson, Oct 1, 2004, in forum: Python
    Replies:
    3
    Views:
    372
    Mirko Zeibig
    Oct 1, 2004
  4. Replies:
    4
    Views:
    340
  5. Henrik Goldman

    Recursing macro preprocessing?

    Henrik Goldman, Oct 21, 2006, in forum: C++
    Replies:
    4
    Views:
    379
    Kaz Kylheku
    Oct 22, 2006
Loading...

Share This Page