如何在页面上查找所有DropDownList控件 [英] How to find all DropDownList Controls on a page

查看:67
本文介绍了如何在页面上查找所有DropDownList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载一个包含所有DropDownList控件名称的下拉列表来自

另一页。


我怎么能够找到那些DropDownList控件? FindControl

方法只会通过id找到某个控件,但我想找到某种类型的所有

控件(在这种情况下是DropDownList)。


是否有比获取页面控制计数更简单的方法,循环浏览页面上的所有控件,检查它们的类型,如果它们是DropDownList

控制,将它们添加到数组中?


谢谢,


Sacha

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

推荐答案

嗨Sacha,


我会按照你最后建议的方式做到这一点......循环遍历所有

控制表格并检查每种类型。





" Sacha Korell" < KO **** @ huntsville.sparta.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP11.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...
我'我试图从另一个页面加载一个包含所有DropDownList控件名称的下拉列表。

我怎样才能找到那些DropDownList控件? FindControl
方法只能通过id找到某个控件,但我想找到某种类型的所有
控件(在这种情况下是DropDownList)。

是否更容易得到页面的控制计数,循环
通过该页面上的所有控件,检查它们的类型,如果它们是一个DropDownList控件,将它们添加到一个数组?

谢谢,

Sacha
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






Ken,


感谢您的快速回复。


当我知道hw_edit中至少有50个控件

时,为什么我的控制计数为0?

" hw_edit"是继承自Page的类。


Dim objPage As New LogmaitWeb.hw_edit


intControlCount = objPage.Controls.Count

谢谢,


Sacha

" Ken Cox [微软MVP]" < BA ************ @ sympatico.ca>在消息中写道

新闻:好的************** @ TK2MSFTNGP09.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...
嗨Sacha,

我会按你最后建议的方式做到这一点......循环浏览表单中的所有
控件并检查每个类型。

Ken

Sacha Korell < KO **** @ huntsville.sparta.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.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...
我正在尝试从另一个页面加载一个包含所有DropDownList控件名称的下拉列表。

我怎样才能找到那些DropDownList控件? FindControl
方法只能通过id找到某个控件,但我想找到某种类型的所有
控件(在这种情况下是DropDownList)。

是否更容易得到页面的控制计数,循环
通过该页面上的所有控件,检查它们的类型,如果它们是一个DropDownList控件,将它们添加到一个数组?

谢谢,

Sacha
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



这是另一种使用反射的方法。您需要获得要检查的页面的类型

,然后将其所有字段拉出来寻找

类型的DropDownList。


这是我使用的代码示例:


使用System;

使用System.Reflection;


命名空间tempcsharp

{

公共类ReflectionStuff

{

public static void GetDropDownControls()

{

System.Reflection.Assembly ass =

System.Reflection.Assembly.GetExecutingAssembly();

输入page = ass.GetType(" tempcsharp.TestPage");

foreach(页面中的FieldInfo信息.GetFields((BindingFlags.Public |

BindingFlags.NonPublic)| BindingFlags.Instance |

indingFlags.DeclaredOnly))

{

if(info.FieldType == typeof(

System.Web.UI.WebControls.DropDownList))

{

Console.WriteLine(info.Name);

}

}

}

}


公共类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>在消息中写道

新闻:%2 *************** @ TK2MSFTNGP11.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,

感谢您的快速回复。

当我知道hw_edit中至少有50个
控件时,为什么控件数量为0?
" hw_edit"是继承自Page的类。

Dim objPage As New LogmaitWeb.hw_edit

intControlCount = objPage.Controls.Count

谢谢,

Sacha

" Ken Cox [微软MVP]" < BA ************ @ sympatico.ca>在消息中写道
新闻:好的************** @ TK2MSFTNGP09.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...
嗨萨莎,
控件并检查每个类型。

Ken

Sacha Korell < KO **** @ huntsville.sparta.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.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...
我正在尝试从另一个页面加载一个包含所有DropDownList控件名称的下拉列表。

我怎样才能找到那些DropDownList控件?
FindControl方法只能通过id找到某个控件,但是我想找到某种类型的所有
控件(在这种情况下是DropDownList)。

是否更容易得到页面的控制计数,循环
通过该页面上的所有控件,检查它们的类型,如果它们是一个DropDownList控件,将它们添加到一个数组?

谢谢,

Sacha
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




这篇关于如何在页面上查找所有DropDownList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆