Skip Navigation Links
技术文档
·网站建设
·软件使用
·图形设计
·程序开发
·网络应用
·电脑技巧
 
公司介绍
·公司简介
·索仕SRCOS网络应用平台
·索仕网站管理系统
·影视广告制作
·联系我们
 
 

递归方式的 FindControl (进阶版)

8/1/2008 5:07:05 PM

一般 FindControl 方法,大都是以 ID 寻找控件的第一阶的子控件(若控件有多载 FindControl 方法则例外)。之前有发表过一篇「递归方式的 FindControl」的文章,它是以递归方式逐层往下去执行 FindControl,找到指定 ID 的控件。
此篇文章是提供进阶版的 FindControl,此方法一样是以递归方式逐层往下去执行 FindControl,不过它不限只能以 ID 去寻找控件,而是指定「型别、属性名称、属性值」去寻找符合的控件。

 1    ''' <summary>
 2递归方式的 FindControl (进阶版)    ''' 递归寻找符合条件的控件。
 3递归方式的 FindControl (进阶版)    ''' </summary>

 4递归方式的 FindControl (进阶版)    ''' <param name="Parent">父控件。</param>
 5递归方式的 FindControl (进阶版)    ''' <param name="Type">欲寻找的控件型别。</param>
 6递归方式的 FindControl (进阶版)    ''' <param name="PropertyName">比对的属性名称。</param>
 7递归方式的 FindControl (进阶版)    ''' <param name="PropertyValue">比对的属性值。</param>

 8    Public Overloads Shared Function FindControlEx(ByVal Parent As System.Web.UI.Control, ByVal Type As System.Type, _
 9递归方式的 FindControl (进阶版)        ByVal PropertyName As StringByVal PropertyValue As ObjectAs Object

10递归方式的 FindControl (进阶版)        Dim oControl As System.Web.UI.Control
11递归方式的 FindControl (进阶版)        Dim oFindControl As Object

12递归方式的 FindControl (进阶版)        Dim oValue As Object
13递归方式的 FindControl (进阶版)
14递归方式的 FindControl (进阶版)        For Each oControl In
 Parent.Controls
15递归方式的 FindControl (进阶版)            If Type.IsInstanceOfType(oControl) Then

16递归方式的 FindControl (进阶版)                '取得属性值
17递归方式的 FindControl (进阶版)                oValue = GetPropertyValue(oControl, PropertyName)
18递归方式的 FindControl (进阶版)                If oValue.Equals(PropertyValue) Then

19递归方式的 FindControl (进阶版)                    Return oControl '型别及属性值皆符合则回传此控件
20递归方式的 FindControl (进阶版)                End If
21递归方式的 FindControl (进阶版)            Else
22递归方式的 FindControl (进阶版)                If oControl.Controls.Count > 0 Then
23递归方式的 FindControl (进阶版)                    '递归往下寻找符合条件的控件
24递归方式的 FindControl (进阶版)                    oFindControl = FindControlEx(oControl, Type, PropertyName, PropertyValue)
25递归方式的 FindControl (进阶版)                    If oFindControl IsNot Nothing Then

26递归方式的 FindControl (进阶版)                        Return oFindControl
27递归方式的 FindControl (进阶版)                    End If

28递归方式的 FindControl (进阶版)                End If
29递归方式的 FindControl (进阶版)            End If
30递归方式的 FindControl (进阶版)        Next
31递归方式的 FindControl (进阶版)        Return Nothing
32递归方式的 FindControl (进阶版)    End Function

33递归方式的 FindControl (进阶版)
34    
''' <summary>
35递归方式的 FindControl (进阶版)    ''' 取得对象的属性值。
36递归方式的 FindControl (进阶版)    ''' </summary>

37递归方式的 FindControl (进阶版)    ''' <param name="Component">具有要撷取属性的对象。</param>
38递归方式的 FindControl (进阶版)    ''' <param name="PropertyName">属性名称。</param>

39    Public Shared Function GetPropertyValue(ByVal Component As ObjectByVal PropertyName As StringAs Object
40递归方式的 FindControl (进阶版)        Dim Prop As PropertyDescriptor = TypeDescriptor.GetProperties(Component).Item(PropertyName)
41递归方式的 FindControl (进阶版)        Return
 Prop.GetValue(Component)
42递归方式的 FindControl (进阶版)    End Function

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

递归方式的 FindControl (进阶版)        Dim oLinkButton As LinkButton
递归方式的 FindControl (进阶版)        oLinkButton 
= CType(FindControlEx(FormView1, GetType(LinkButton), "CommandName""Insert"), LinkButton)

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

递归方式的 FindControl (进阶版)        Dim oButtonControl As IButtonControl
递归方式的 FindControl (进阶版)        oButtonControl 
= CType(FindControlEx(FormView1, GetType(IButtonControl), "CommandName""Insert"), IButtonControl)

 

作者:jeff377 来源:博客园
 
 
 
昆明索仕科技开发有限公司 版权所有 Copyright© 2002-2010 Kunming Source Technology Exploitive Co.,LTD. All Rights Reserved.
电话:0871-5627877 业务QQ:163871 联系我们
本站基于:索仕网站信息管理系统建设 版本 2.0.4325