首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

Asp.net—递归方式的FindControl

一般 FindControl 方法,大都是以 ID 寻找控件的第一阶的子控件(若控件有多载 FindControl 方法则例外)。之前有发表过一篇「
递归方式的 FindControl」的文章,它是以递归方式逐层往下去执行 FindControl,找到指定 ID 的控件。
此篇文章是提供进阶版的 FindControl,此方法一样是以递归方式逐层往下去执行 FindControl,不过它不限只能以 ID 去寻找控件,而是指定「型别、属性名称、属性值」去寻找符合的控件。
 1    ''' <summary>
 2    ''' 递归寻找符合条件的控件。
 3    ''' </summary>
 4    ''' <param name="Parent">父控件。</param>
 5    ''' <param name="Type">欲寻找的控件型别。</param>
 6    ''' <param name="PropertyName">比对的属性名称。</param>
 7    ''' <param name="PropertyValue">比对的属性值。</param>

 8    Public Overloads Shared Function FindControlEx(ByVal Parent As System.Web.UI.Control, ByVal Type As System.Type, _
 9        ByVal PropertyName As StringByVal PropertyValue As ObjectAs Object
10        Dim oControl As System.Web.UI.Control
11        Dim oFindControl As Object
12        Dim oValue As Object
13
14        For Each oControl In Parent.Controls
15            If Type.IsInstanceOfType(oControl) Then
16                '取得属性值
17                oValue = GetPropertyValue(oControl, PropertyName)
18                If oValue.Equals(PropertyValue) Then
19                    Return oControl '型别及属性值皆符合则回传此控件
20                End If
21            Else
22                If oControl.Controls.Count > 0 Then
23                    '递归往下寻找符合条件的控件
24                    oFindControl = FindControlEx(oControl, Type, PropertyName, PropertyValue)
25                    If oFindControl IsNot Nothing Then
26                        Return oFindControl
27                    End If
28                End If
29            End If
30        Next
31        Return Nothing
32    End Function

33
34    ''' <summary>
35    ''' 取得对象的属性值。
36    ''' </summary>
37    ''' <param name="Component">具有要撷取属性的对象。</param>
38    ''' <param name="PropertyName">属性名称。</param>

39    Public Shared Function GetPropertyValue(ByVal Component As ObjectByVal PropertyName As StringAs Object
40        Dim Prop As PropertyDescriptor = TypeDescriptor.GetProperties(Component).Item(PropertyName)
41        Return Prop.GetValue(Component)
42    End Function

例如我们要寻找 FormView 控件中一个 CommandName="Insert" 的 LinkButton(ID="FormView1") 控件,则可以如下呼叫 FindControlEx 方法。

        Dim oLinkButton As LinkButton
        oLinkButton 
= CType(FindControlEx(FormView1, GetType(LinkButton), "CommandName""Insert"), LinkButton)

如果你要寻找的按钮有可能为 Button、LinkButton、ImageButton任一型别的按钮,因为这些按钮都有实作 System.Web.UI.WebControls.IButtonControl 接口,所以也可以利用 IButtonControl 接口去寻找更有弹性。

        Dim oButtonControl As IButtonControl
        oButtonControl 
= CType(FindControlEx(FormView1, GetType(IButtonControl), "CommandName""Insert"), IButtonControl)
【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航