473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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

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.Reflecti on;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownCont rols()
{
System.Reflecti on.Assembly ass =
System.Reflecti on.Assembly.Get ExecutingAssemb ly();
Type page = ass.GetType( "tempcsharp.Tes tPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Pu blic |
BindingFlags.No nPublic ) | BindingFlags.In stance |
indingFlags.Dec laredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.W ebControls.Drop DownList ) )
{
Console.WriteLi ne( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.P age
{
public System.Web.UI.W ebControls.Drop DownList ddl1;
protected System.Web.UI.W ebControls.Drop DownList ddl2;
protected System.Web.UI.W ebControls.Drop DownList ddl3;
protected System.Web.UI.W ebControls.Text Box txt1;
protected System.Web.UI.W ebControls.Drop DownList ddl4;
protected System.Web.UI.W ebControls.Drop DownList ddl5;
protected System.Web.UI.W ebControls.Drop DownList ddl6;
}
}

"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


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



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******** ******@TK2MSFTN GP10.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.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


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****@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
| >>
| >
|
|
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_fdrs ucks.com> wrote in message
news:uI******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP10.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.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
>>>
>>
>
>


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_fdrs ucks.com> wrote in message
news:uc******** ******@tk2msftn gp13.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.Reflecti on;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownCont rols()
{
System.Reflecti on.Assembly ass =
System.Reflecti on.Assembly.Get ExecutingAssemb ly();
Type page = ass.GetType( "tempcsharp.Tes tPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Pu blic |
BindingFlags.No nPublic ) | BindingFlags.In stance |
indingFlags.Dec laredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.W ebControls.Drop DownList ) )
{
Console.WriteLi ne( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.P age
{
public System.Web.UI.W ebControls.Drop DownList ddl1;
protected System.Web.UI.W ebControls.Drop DownList ddl2;
protected System.Web.UI.W ebControls.Drop DownList ddl3;
protected System.Web.UI.W ebControls.Text Box txt1;
protected System.Web.UI.W ebControls.Drop DownList ddl4;
protected System.Web.UI.W ebControls.Drop DownList ddl5;
protected System.Web.UI.W ebControls.Drop DownList ddl6;
}
}

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



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.Tes tPage" );

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_fdrs ucks.com> wrote in message
news:uc******** ******@tk2msftn gp13.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.Reflecti on;

namespace tempcsharp
{
public class ReflectionStuff
{
public static void GetDropDownCont rols()
{
System.Reflecti on.Assembly ass =
System.Reflecti on.Assembly.Get ExecutingAssemb ly();
Type page = ass.GetType( "tempcsharp.Tes tPage" );
foreach( FieldInfo info in page.GetFields( ( BindingFlags.Pu blic |
BindingFlags.No nPublic ) | BindingFlags.In stance |
indingFlags.Dec laredOnly ) )
{
if ( info.FieldType == typeof(
System.Web.UI.W ebControls.Drop DownList ) )
{
Console.WriteLi ne( info.Name );
}
}
}
}

public class TestPage : System.Web.UI.P age
{
public System.Web.UI.W ebControls.Drop DownList ddl1;
protected System.Web.UI.W ebControls.Drop DownList ddl2;
protected System.Web.UI.W ebControls.Drop DownList ddl3;
protected System.Web.UI.W ebControls.Text Box txt1;
protected System.Web.UI.W ebControls.Drop DownList ddl4;
protected System.Web.UI.W ebControls.Drop DownList ddl5;
protected System.Web.UI.W ebControls.Drop DownList ddl6;
}
}

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



Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1668
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 i change the text displayed in the label in the same row. hier is my code: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web;
2
428
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 click the submit button? I've tryed : pnlName.Controls
4
5482
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
4
7459
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 placeholder,the user selects the required tags from the dropdownlists (if 5 Xml tags,then 5 dropdownlists each containing 5 xml tags) and now the btnSave button is selected which extracts the user selection form the dropdownlists.
2
3760
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 <asp:EditCommandColumn> I want either a textbox or dropdown to appear when I check what is being edited. I thought I could create the dropdown on the fly and add it to the datagrid as below but It's not appearing. I imagined I could populate the dropdown
1
6562
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 have a separate submit button which should validate the other fields. This page on MSDN does what I want - almost! ...
3
2072
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 SelectedItem of the DDL (as retrieved from any of the supplied methods) is always the top item regardless of which one is actually selected. The odd thing is that any resubmissions of the form produce the correct SelectedItem.
4
1978
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 can't access on the selected values of those dynamic dropdownlists.
2
3304
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 details first. The DropDownList is actually a custom control called DropDownListWithCommandEvent that inherits from DropDownList. The reason I have created this is to create a DropDownList that will bubble a Command event to the containing...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.