[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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

0
rwallacej
Asked:
rwallacej
  • 3
  • 2
1 Solution
 
Praveen VenuCommented:
Are you using a masterpage?
0
 
rwallacejAuthor Commented:
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)
0
 
rwallacejAuthor Commented:
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

0
 
Praveen VenuCommented:
why can not put a panel and put all your controls inside the panel and then loop

Panel1.Controls
0
 
rwallacejAuthor Commented:
thank-you very much, this works
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now