Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2004, 02:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Looping Through Controls

Is there a way to loop through all the controls in a PlaceHolder to check to see if they exist?

 
Old February 9th, 2004, 12:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can loop thru all controls of an control by accessing the <control>.Controls collection.
 
Old March 4th, 2004, 01:40 PM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to johnpirkey Send a message via MSN to johnpirkey Send a message via Yahoo to johnpirkey
Default

This is a perfect place to employ Recursion. Most likely there's an HTML table containing the controls, each cell will also be returned as an object in the .Controls property collection. so, not only will you have to iterate through the placeholder's controls, but each of their controls as well. Here's a skeleton of a recursive function i've used to do exactly that.

The purpose of this function was to iterate through a dynamically built list of checkboxes and build a string of their values (but since they dont have values, i'm cheating and storing their ID value in the CssClass property, anyway...).

I'm sure you can get what you need out of this:

Code:
Function GetFieldDefs(ByVal sFieldDefs As String, Optional ByVal ctl As Control = Nothing) As String
Dim chk As CheckBox
Dim ctl2 As Control

'this is a recursive function to walk the tree structure of the control hierarchy
If ctl Is Nothing Then
    For Each ctl2 In Me.Controls
        If ctl2.HasControls Then
            sFieldDefs = GetFieldDefs(sFieldDefs, ctl2)
        Else
            'there are no more child controls - lets walk through the current set
            If ctl2.GetType.ToString = "System.Web.UI.WebControls.CheckBox" Then
                chk = CType(ctl2, System.Web.UI.WebControls.CheckBox)
                If chk.Checked = True And chk.CssClass <> "" Then
                    sFieldDefs += chk.CssClass & ","
                End If
            End If
        End If
    Next
Else
    For Each ctl2 In ctl.Controls
        If ctl2.HasControls Then
            sFieldDefs = GetFieldDefs(sFieldDefs, ctl2)
        Else
            'there are no more child controls - lets walk through the current set
            If ctl2.GetType.ToString = "System.Web.UI.WebControls.CheckBox" Then
                chk = CType(ctl2, System.Web.UI.WebControls.CheckBox)
                If chk.Checked = True And chk.CssClass <> "" Then
                    sFieldDefs += chk.CssClass & ","
                End If
            End If
        End If
    Next
End If
    Return sFieldDefs
End Function
Hope this helps.

john

MCSD(VB6)
http://www.stlvbug.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Looping controls and array redim mega Excel VBA 2 April 19th, 2005 11:57 AM
Looping Form Generation - Refering to Controls IP076 Pro PHP 1 December 10th, 2004 08:13 PM
Looping through Controls on a Page jbenson001 ASP.NET 1.x and 2.0 Application Design 2 December 4th, 2003 03:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.