473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 5275
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
2
by: Mike | last post by:
Can someone help me figure this out... I've created dropdown boxes dynamically and added them to a panel on my page. how do i get the selectedvalue of the dropdownlist from the panel when i...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
2
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
1
by: Liz | last post by:
I have a page with several dropdownlists, several text boxes and several buttons which perform calculations. I need to validate one dropdownlist (not the whole page) with the click of one button. I...
3
by: Sam C | last post by:
Hi, I have an ASP.Net page which has a DropDownList on it. The DDL is populated via a method which is called from the Page_Load if IsPostBack = False. When the form is submitted the...
4
by: TheHach | last post by:
Hi. (For information, i'm working in VB.NET) I have a datagrid on my page. On its creation, I add a dropdownlist in each cell, with a different ID each time. This works fine. But now, I...
2
by: jnoody | last post by:
The problem I am having is with the SelectedIndexChanged event not always firing or the SelectedIndex property not being correct when the event does fire. The code is below, but here are some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.