Hi Sacha,
I suspect you're not looking in the right collection for the controls that
you are after. It helps to put tracing on to see the hierarchy of controls
on the ASP.NET page. As you'll see, the Form controls are inside containers.
Also, if you are using other containers you might have to drill down
further.
I've put some code below that shows how you might get the total count of
controls and the DDLs. (Watch for bad line wrapping.)
Let us know if this helps?
Ken
Microsoft MVP [ASP.NET]
Toronto
Public Class hw_edit
Inherits System.Web.UI.P age
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
End Sub
Protected WithEvents DropDownList1 As
System.Web.UI.W ebControls.Drop DownList
Protected WithEvents DropDownList2 As
System.Web.UI.W ebControls.Drop DownList
Protected WithEvents DropDownList3 As
System.Web.UI.W ebControls.Drop DownList
Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Public intControlCount As Integer
Public intDDLCount As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Page_PreRender( ByVal sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.PreRende r
Dim cntrl As Control
For Each cntrl In Page.Controls(1 ).Controls
If TypeOf (cntrl) Is System.Web.UI.W ebControls.Drop DownList Then
intDDLCount = intDDLCount + 1
End If
Next
Label1.Text = "Number of DDLs: " & intDDLCount.ToS tring & _
"<br>Number of Controls: " &
Page.Controls(1 ).Controls.Coun t.ToString
End Sub
End Class
<%@ Page Language="vb" trace="true" AutoEventWireup ="false"
Codebehind="cnt rlscount.aspx.v b" Inherits="p4320 work.hw_edit"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>cntrlsco unt</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body MS_POSITIONING= "FlowLayout ">
<form id="Form1" method="post" runat="server">
<p>
<asp:dropdownli st id="DropDownLis t1"
runat="server"> </asp:dropdownlis t>
<asp:dropdownli st id="DropDownLis t2"
runat="server"> </asp:dropdownlis t>
<asp:dropdownli st id="DropDownLis t3"
runat="server"> </asp:dropdownlis t></p>
<p>
<asp:label id="Label1" runat="server"> Label</asp:label></p>
</form>
</body>
</html>
"Sacha Korell" <ko****@huntsvi lle.sparta.com> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Ken,
Thanks for your quick reply.
Why would I get a control count of 0 when I know I have at least 50
controls in hw_edit?
"hw_edit" is the class that inherits from Page.
Dim objPage As New LogmaitWeb.hw_e dit
intControlCount = objPage.Control s.Count
Thanks,
Sacha
"Ken Cox [Microsoft MVP]" <BA************ @sympatico.ca> wrote in message
news:Ok******** ******@TK2MSFTN GP09.phx.gbl... Hi Sacha,
I would do it the way you suggested at the end... loop through all the
controls in the form and check each for the type.
Ken
"Sacha Korell" <ko****@huntsvi lle.sparta.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. I'm trying to load a drop-down list with all DropDownList control names
from another page.
How would I be able to find those DropDownList controls? The FindControl
method will only find a certain control by id, but I want to find all
controls of a certain type (DropDownList in this case).
Is there an easier way than to get a control count of the page, loop
through all controls on that page, examine their type and, if they're a
DropDownList control, adding them to an array?
Thanks,
Sacha