FindControl 找控件,方法总结

FindControl 是 ASP.NET 工程師十分常用的 Method,但初學者應該常常會遇到使用 FindControl 卻找不到 Control 的狀況!

首先,在 ASP.NET 中有所謂 NamingContainer (命名容器) 的觀念,在使用 Data-bound Control ( 資料控制項 ) 時,會用到 ItemTemplate 之類(ITemplate)的標籤,裡面還會包含許多 Control,這些包含在 Template 裡面的 Control 其實是跟原本頁面(Page)中的控制項是不同階層的!而這些被 ITemplate 包含的 Controls 其 NamingContainer 就是這個 Data-bound Control 的每一個 ItemTemplate!

在這種情況下,若要在 Code behind 中使用 Page.FindControl 想直接找到這些 Data-bound Control 的 Template 中的 Control 的話,就沒辦法直接用 Control ID 找到這個 Control。

例如:

 

[code:html]
        < asp:repeater ID ="Repeater1" runat ="server" >
            < ItemTemplate >

                < asp:TextBox ID ="TextBox1" runat ="server" Text ="Text on row" />

            </ ItemTemplate >
        </ asp:repeater >
[/code]

在 Code behind 的 Page_Load 事件中要尋找 Repeater1 中第一個 TextBox1 時,使用以下的程式碼就會出錯,因為找不到 TextBox1:

 

[code:c#]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load( object sender, EventArgs e)
    {
        System.Collections.ArrayList a = new ArrayList();
        a.Add("List 1");
        a.Add("List 2");
        Repeater1.DataSource = a;
        Repeater1.DataBind();
        TextBox txt1 = (TextBox)Page.FindControl("TextBox1");  // txt1 會是 null
        Response.Write(txt1.Text); // 因為 txt1 是 null 而無法取得 txt1.Text 屬性而發生例外(Exception)
    }
}
[/code]

如果要透過 FindControl 找到控制項的話,我是有以下 4 種技巧分享給大家:

1. 透過實做 Repeater1 的 ItemDataBound 事件來取得每一個 ItemTemplate 中 TextBox1 的控制項

這是最常見的用法!

 

[code:c#]
    protected void Repeater1_ItemDataBound( object sender, RepeaterItemEventArgs e)
    {
        TextBox txt1 = (TextBox)e.Item.FindControl("TextBox1");
        Response.Write(txt1.Text);
    }
[/code]

2. 透過 Repeater1.Items[0].FindControl("TextBox1") 找到第一個 ItemTemplate 中的 TextBox1 控制項

這是取得第一個 ItemTemplate 中的 TextBox1 的常見用法!

3. 透過 Page.FindControl("Repeater1$ctl00$TextBox1") 找到第一個 ItemTemplate 中的 TextBox1 控制項

 這算是使用 FindControl 的小技巧,因為 ASP.NET 的控制項如果沒有 ID 的話,都是從 ctl00 開始算起的。
 因為 ItemTemplate 是 NamingContainer 但卻沒有設定 ID,所以第一筆就是 ctl00 第二筆就是 ctl01 依此類推。

4. 透過 FindControl<TextBox>("TextBox1") 找到整個頁面中第一個出現的 TextBox1 控制項(不一定在 Repeater1 裡面)

這是透過一個自訂的泛型遞迴方法(Generic Recursive Method)達成。
 
要使用這段程式比需將以下的程式碼複製到你的頁面的類別(Code behind)中。
( 以下程式碼參考自:http://blogs.interfacett.com/michael-palermo/2007/4/13/recursive-findcontrolt.html  )

[code:c#]
    public T FindControl<T>( string id) where T : Control
    {
        return FindControl<T>(Page, id);
    }

    public static T FindControl<T>(Control startingControl, string id) where T : Control
    {
        // 取得 T 的預設值,通常是 null
        T found = default (T);

        int controlCount = startingControl.Controls.Count;

        if (controlCount > 0)
        {
            for (int i = 0; i < controlCount; i++)
            {
                Control activeControl = startingControl.Controls[i];
                if (activeControl is T)
                {
                    found = startingControl.Controls[i] as T;
                    if (string .Compare(id, found.ID, true ) == 0) break ;
                    else found = null ;
                }
                else
                {
                    found = FindControl<T>(activeControl, id);
                    if (found != null ) break ;
                }
            }
        }
        return found;
    }
[/code]

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ASPX用户控件是一种用于在ASP.NET Web应用程序中创建可重复使用的自定义界面元素的技术。使用ASPX用户控件可以将常见的界面元素组合成单个自定义控件,以实现更高效的开发和维护。 要使用ASPX用户控件,首先需要创建一个用户控件的ASPX文件。在该文件中,可以定义用户控件的布局和内容,可以包含HTML、CSS和ASP.NET的服务器控件。 接下来,在Web页面或其他ASP.NET控件中使用用户控件。可以使用<%@ Register %>指令来注册用户控件,以便在页面中引用。然后,可以在页面的HTML部分或代码部分使用<%@ Reference %>指令来引用已注册的用户控件。 一旦用户控件被引用并注册,可以像使用其他服务器控件一样在页面上使用它。可以使用<uc:ControlName>标签来实例化用户控件,并在标签内指定用户控件的属性值。 用户控件可以包含代码-behind文件,用于处理用户控件的逻辑和事件响应。可以将代码-behind文件与用户控件的ASPX文件关联起来,并在其中编写相关的代码。 在页面后台代码中,可以通过使用类似于查其他服务器控件的方式来访问和操作用户控件。可以使用FindControl()方法来获取用户控件的引用,并使用它来访问用户控件的属性和方法。 在开发ASPX用户控件时,可以使用多种开发工具和技术,如Visual Studio、C#或VB.NET等。可以将用户控件编译为DLL文件,并将其放置在Web应用程序的Bin文件夹中。这样,用户控件就可以像其他服务器控件一样进行部署和使用。 总而言之,ASPX用户控件是一种用于创建可重复使用的自定义界面元素的技术。使用它可以实现更高效的开发和维护,并提供更高级的灵活性和扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值