Link to home
Start Free TrialLog in
Avatar of rwallacej
rwallacej

asked on

Iterate through all custom controls

Hi

I have a page with custom controls on it. The custom control is called UserControls_TestControlWithProperty. There may be one, more or many of the UserControls_TestControlWithProperty on the page.
I want to iterate through all the controls on page, and if the control is of type UserControls_TestControlWithProperty do something with it. Unfortunately the code I have tried /changed isn't finding the controls of this type. See some code below and output from the code, hopefully this will assist in finding the UserControls_TestControlWithProperty objects so I can cast and modify them.

Thank-you.
Dim r As String = "DETAILS OF ALL TAG OBJECTS"
            For Each c As Control In Me.Controls
                Response.Write(c.ToString & "<p>")
                Response.Write(c.ClientID & "<p>")
 
                'If TypeOf (c) Is UserControls_TestControlWithProperty Then
                'r = r & vbCrLf & DirectCast(c, UserControls_TestControlWithProperty).TS.ToString
                'End If
 
 
                If TypeOf (c) Is System.Web.UI.HtmlControls.HtmlForm Then
                    Dim cd As HtmlForm = DirectCast(c, HtmlForm)
                    For Each d As Control In cd.Controls
                        Response.Write("--> " & d.ToString & ".....     CLIENTID: " & d.ClientID & "<p>")
                        Response.Write("----> TYPE: " & d.GetType.ToString & "<p>")
 
                        If TypeOf d Is LiteralControl Then
                            Dim dc As LiteralControl = DirectCast(d, LiteralControl)
 
                            For Each dcc As Control In dc.Controls
                                Response.Write("---> --> -->" & dcc.ToString & ".     " & dcc.ClientID & "<p>")
                            Next
                        ElseIf TypeOf (c) Is UserControls_TestControlWithProperty Then
                            Response.Write("<h1>UserControls_TestControlWithProperty </h1>")
                            'never get in here
                            Response.Write("<p><b>" & DirectCast(c, UserControls_TestControlWithProperty).TS.ToString & "</b><p>")
                        ElseIf TypeOf (c) Is ASP.tests_usercontrols_testcontrolwithproperty_ascx Then
                            'never get in here
                            Response.Write("this is it here.......................<p>")
                        End If
 
 
 
                    Next
                End If
            Next
 
 
 
OUTPUTS
System.Web.UI.LiteralControl
ctl02
 
System.Web.UI.HtmlControls.HtmlHead
 
ctl00
 
System.Web.UI.LiteralControl
 
ctl03
 
System.Web.UI.HtmlControls.HtmlForm
 
form1
 
--> System.Web.UI.LiteralControl..... CLIENTID: ctl04
 
----> TYPE: System.Web.UI.LiteralControl
 
--> ASP.tests_usercontrols_testcontrolwithproperty_ascx..... CLIENTID: TestControlWithProperty1
 
----> TYPE: ASP.tests_usercontrols_testcontrolwithproperty_ascx
 
--> System.Web.UI.LiteralControl..... CLIENTID: ctl05
 
----> TYPE: System.Web.UI.LiteralControl
 
--> System.Web.UI.WebControls.Button..... CLIENTID: Button1
 
----> TYPE: System.Web.UI.WebControls.Button
 
--> System.Web.UI.LiteralControl..... CLIENTID: ctl06
 
----> TYPE: System.Web.UI.LiteralControl
 
--> System.Web.UI.WebControls.TextBox..... CLIENTID: TextBox1
 
----> TYPE: System.Web.UI.WebControls.TextBox
 
--> System.Web.UI.LiteralControl..... CLIENTID: ctl07
 
----> TYPE: System.Web.UI.LiteralControl
 
System.Web.UI.LiteralControl
 
ctl08

Open in new window

Avatar of Praveen Venu
Praveen Venu
Flag of India image

Are you using a masterpage?
Avatar of rwallacej
rwallacej

ASKER

hi,
no I am not using master page, this is just a plain .ASPX page with controls on it (I will use a master page eventually though)
here is the contents of it
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TestingTagControl.aspx.vb" Inherits="Tests_TestingTagControl" %>
 
<%@ Register src="UserControls/TestControlWithProperty.ascx" tagname="TestControlWithProperty" tagprefix="uc1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <uc1:TestControlWithProperty ID="TestControlWithProperty1" runat="server" /><p></p>
<%--        <uc1:TestControlWithProperty ID="TestControlWithProperty2" runat="server" /><p></p>
        <uc1:TestControlWithProperty ID="TestControlWithProperty3" runat="server" /><p>
--%>            <asp:Button ID="Button1" runat="server" Text="Button" />
<%--        </p>
--%>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="248px" 
        Width="1065px"></asp:TextBox>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Praveen Venu
Praveen Venu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank-you very much, this works