posts - 57, comments - 257, trackbacks - 14, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

递归方式的 FindControl (进阶版)

Posted on 2008-05-13 03:10 jeff377 阅读(1568) 评论(6)  编辑 收藏 所属分类: ASP.NET 基础
一般 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)

 

Tag标签: FindControl

Feedback

#1楼    回复  引用  查看    

2008-05-13 08:33 by yangjun      
学习了。路过

#2楼    回复  引用  查看    

2008-05-13 08:46 by ddddddddddddddddwe [未注册用户]
麻烦转成c#

#3楼    回复  引用  查看    

2008-05-13 09:18 by 狼Robot      
学习

#4楼    回复  引用  查看    

2008-05-13 09:35 by ASP.NET CMS [未注册用户]
学习
另外问2楼为什么要专成c#
学习思想不就好了,难道你要用现成的代码?

#5楼 [楼主]   回复  引用  查看    

2008-05-13 09:48 by jeff377      
@ddddddddddddddddwe
你直接使用下面網址提供的工具,就有辨法把 VB.NET 轉為 C# 語法。
http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx

#6楼    回复  引用  查看    

2008-05-13 17:55 by nicye      

我以前也写过类似的函数,贴出来:

#region GetWebControl 递归获取 page 第一个 ID 的 WEB控件
/// <summary>
/// 从 HttpContext.Current.Handler 中递归获取第一个 ID 的 WEB控件
/// </summary>
/// <param name="id">WEB控件的 ID</param>
/// <returns>WEB控件</returns>
public static System.Web.UI.Control GetWebControl(string id) {
    
if (HttpContext.Current == nullreturn null;
    System.Web.UI.Page page 
= HttpContext.Current.Handler as System.Web.UI.Page;
    
if (page == null || string.IsNullOrEmpty(id)) return null;
    System.Web.UI.Control val 
= page.FindControl(id);
    
return val == null ? GetWebControl(id, page.Controls) : val;
}
static System.Web.UI.Control GetWebControl(string id, System.Web.UI.ControlCollection controls) {
    System.Web.UI.Control val 
= null;
    
foreach (System.Web.UI.Control control in controls) {
        val 
= control.FindControl(id);
        
if (val != nullreturn val;
        
if (control.Controls.Count > 0) {
            val 
= GetWebControl(id, control.Controls);
            
if (val != nullreturn val;
        }
    }
    
return val;
}
#endregion

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-05-13 12:38 编辑过
 
另存  打印
最新IT新闻:
· 《福布斯》:暴雪的新一波完美风暴已经到来
· 中国互联网公司在哪儿
· 微软:Silverlight内容也可被搜索引擎检索
· 内置AI技术 三星聪明微波炉还能上网
· 盛大设文学公司 原新浪博客负责人侯小强任CEO