468,161 Members | 1,956 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

How to find all DropDownList Controls on a page

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
Nov 18 '05 #1
10 4750
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


Nov 18 '05 #2
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

Nov 18 '05 #3
Here is another approach using reflection. You will need to get the type of
the page you want to check, and then pull all its field out looking for the
type of DropDownList.

Here is the code sample I used:

using System;
using System.Reflection;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownControls()
{
System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
Type page = ass.GetType( "tempcsharp.TestPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Public |
BindingFlags.NonPublic ) | BindingFlags.Instance |
indingFlags.DeclaredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.WebControls.DropDownList ) )
{
Console.WriteLine( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.Page
{
public System.Web.UI.WebControls.DropDownList ddl1;
protected System.Web.UI.WebControls.DropDownList ddl2;
protected System.Web.UI.WebControls.DropDownList ddl3;
protected System.Web.UI.WebControls.TextBox txt1;
protected System.Web.UI.WebControls.DropDownList ddl4;
protected System.Web.UI.WebControls.DropDownList ddl5;
protected System.Web.UI.WebControls.DropDownList ddl6;
}
}

"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


Nov 18 '05 #4
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



Nov 18 '05 #5
Ken,

I think he is wanting the controls on a page that isn't instantiated. He is
on Page1.aspx and wants to find all the controls on Page2.aspx. Even if he
were to create an instance of the class, the Controls collection will still
be empty as the controls aren't added to the Control tree in the
constructor. (Or atleast I am pretty sure they aren't)

bill
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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


Nov 18 '05 #6
you need to do more than create the page, it needs the init routine called
(plus also all the pre inint steps). also you are creating an instance of
the codebehind page which usually will not have any controls in it (as they
are created by the aspx page - which is compiled into a new class)

-- bruce (sqlwork.com)
"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
| >>
| >
|
|
Nov 18 '05 #7
That's exactly what I'm trying to do and I'm already finding out that it is
not as easy as I initially thought.

The idea is to create user-configurable dropdowns. In other words, let the
user (in this case an application administrator) select the screen name
(from a drop-down), in the autopostback of that dropdown find all
DropDownList controls for the screen name selected by the user, then after
the user selects the dropdown name, present a grid with the values in that
DropoDownList that the user can add to (or delete from if the value hadn't
been used). Values for dropdown lists are stored in a lookup table.

Is there no way to find the DropDownList control names from the class
definitions? My work-around at this time is to store DropDownList names and
their associated page names in a separate table (only for a capability demo
at this time), but that would be a pain in the you know what to have to add
a value to that table everytime a developer adds a DropDownList to a page
(or creates new pages for that matter.)

Thanks for eveyone's help so far. I haven't given up yet. Maybe we can still
find a way to do this.

Sacha
"William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
news:uI**************@TK2MSFTNGP14.phx.gbl...
Ken,

I think he is wanting the controls on a page that isn't instantiated. He
is
on Page1.aspx and wants to find all the controls on Page2.aspx. Even if
he
were to create an instance of the class, the Controls collection will
still
be empty as the controls aren't added to the Control tree in the
constructor. (Or atleast I am pretty sure they aren't)

bill
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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
>>>
>>
>
>


Nov 18 '05 #8
Thanks for the code sample, Bill. That looks like it should work

Let me see if I can translate your code to VB.NET and use it in my
application.

Sacha
"William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
Here is another approach using reflection. You will need to get the type
of
the page you want to check, and then pull all its field out looking for
the
type of DropDownList.

Here is the code sample I used:

using System;
using System.Reflection;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownControls()
{
System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
Type page = ass.GetType( "tempcsharp.TestPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Public |
BindingFlags.NonPublic ) | BindingFlags.Instance |
indingFlags.DeclaredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.WebControls.DropDownList ) )
{
Console.WriteLine( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.Page
{
public System.Web.UI.WebControls.DropDownList ddl1;
protected System.Web.UI.WebControls.DropDownList ddl2;
protected System.Web.UI.WebControls.DropDownList ddl3;
protected System.Web.UI.WebControls.TextBox txt1;
protected System.Web.UI.WebControls.DropDownList ddl4;
protected System.Web.UI.WebControls.DropDownList ddl5;
protected System.Web.UI.WebControls.DropDownList ddl6;
}
}

"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
>>
>



Nov 18 '05 #9
It actually solved another problem I had already foreseen. I didn't know how
to get the type of the page name selected by the user, but your line

Type page = ass.GetType( "tempcsharp.TestPage" );

solved that problem as all I have to do now is append the class name of the
page selected and substitute it into "TestPage" and it will be totally
dynamic.

Great piece of work!

Thanks again,

Sacha
"William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
Here is another approach using reflection. You will need to get the type
of
the page you want to check, and then pull all its field out looking for
the
type of DropDownList.

Here is the code sample I used:

using System;
using System.Reflection;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownControls()
{
System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
Type page = ass.GetType( "tempcsharp.TestPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Public |
BindingFlags.NonPublic ) | BindingFlags.Instance |
indingFlags.DeclaredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.WebControls.DropDownList ) )
{
Console.WriteLine( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.Page
{
public System.Web.UI.WebControls.DropDownList ddl1;
protected System.Web.UI.WebControls.DropDownList ddl2;
protected System.Web.UI.WebControls.DropDownList ddl3;
protected System.Web.UI.WebControls.TextBox txt1;
protected System.Web.UI.WebControls.DropDownList ddl4;
protected System.Web.UI.WebControls.DropDownList ddl5;
protected System.Web.UI.WebControls.DropDownList ddl6;
}
}

"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
>>
>



Nov 18 '05 #10
Hey Bill,

Thanks for the info. Good stuff!

Ken

"William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
news:uI**************@TK2MSFTNGP14.phx.gbl...
Ken,

I think he is wanting the controls on a page that isn't instantiated. He
is
on Page1.aspx and wants to find all the controls on Page2.aspx. Even if
he
were to create an instance of the class, the Controls collection will
still
be empty as the controls aren't added to the Control tree in the
constructor. (Or atleast I am pretty sure they aren't)

bill
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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
>>>
>>
>
>



Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by DotNetJunkies User | last post: by
4 posts views Thread by TheHach | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.