We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Iterate through all custom controls

rwallacej
rwallacej asked
on
Medium Priority
318 Views
Last Modified: 2012-06-21
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

Comment
Watch Question

Praveen VenuTechnical Project Manager

Commented:
Are you using a masterpage?

Author

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)

Author

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

Technical Project Manager
Commented:
why can not put a panel and put all your controls inside the panel and then loop

Panel1.Controls

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thank-you very much, this works
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.