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.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList2 As
System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList3 As
System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) 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.EventArgs) Handles MyBase.PreRender
Dim cntrl As Control
For Each cntrl In Page.Controls(1).Controls
If TypeOf (cntrl) Is System.Web.UI.WebControls.DropDownList Then
intDDLCount = intDDLCount + 1
End If
Next
Label1.Text = "Number of DDLs: " & intDDLCount.ToString & _
"<br>Number of Controls: " &
Page.Controls(1).Controls.Count.ToString
End Sub
End Class
<%@ Page Language="vb" trace="true" AutoEventWireup="false"
Codebehind="cntrlscount.aspx.vb" Inherits="p4320work.hw_edit"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>cntrlscount</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<p>
<asp:dropdownlist id="DropDownList1"
runat="server"></asp:dropdownlist>
<asp:dropdownlist id="DropDownList2"
runat="server"></asp:dropdownlist>
<asp:dropdownlist id="DropDownList3"
runat="server"></asp:dropdownlist></p>
<p>
<asp:label id="Label1" runat="server">Label</asp:label></p>
</form>
</body>
</html>
"Sacha Korell" <ko****@huntsville.sparta.com> wrote in message
news:%2***************@TK2MSFTNGP11.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_edit
intControlCount = objPage.Controls.Count
Thanks,
Sacha
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Ok**************@TK2MSFTNGP09.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****@huntsville.sparta.com> wrote in message
news:%2****************@TK2MSFTNGP11.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