標籤:
FindControl 方法和Javascript 中的 getElementById(string) 非常相似。
FindControl的使用方法 Control.FindControl (String):在當前的命名容器中搜尋帶指定 id
參數的伺服器控制項。
Eg:
<form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server">TextBox</asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button"OnClick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div> </form>
如上段代碼中,我們想定位到TextBox1 就可以用 Page.Control[0].FindControl("form1").FindControl("TextBox1")……
也可以是
protected void Button1_Click(object sender, EventArgs e)
{
//Control c = this.FindControl("TextBox1");
//TextBox tb= (TextBox)c;
//FindControl返回的是一個Control類型的控制項,需要強制類型轉化成TextBox類型
TextBox tb=(TextBox)this.FindControl("TextBox1");
this.Label1.Text = tb.Text;
}
當TextBox1放到其他控制項裡時:
<div> <asp:Panel ID="Panel1" runat="server" Height="50px" ;125px"> <asp:TextBox ID="TextBox1" runat="server">TextBox</asp:TextBox> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Button"OnClick="Button1_Click" /> </asp:Panel> </div>
當TextBox1放到Panel裡,似乎沒什麼影響 TextBox tb=(TextBox)this.FindControl
("TextBox1"),當查看生存頁面的HTML代碼是發現,TextBox的ID並沒有改變,所以可以獲得
TextBox1。
當TextBox1放到DataGrid中
<asp:DataGrid ID="dg1" runat="server"OnSelectedIndexChanged="dg1_SelectedIndexChanged"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:ButtonColumn CommandName="Select" Text="選擇"></asp:ButtonColumn> </Columns> </asp:DataGrid>
這時候this.FindControl("TextBox1")==null,無法獲得TextBox1,查看產生頁面HTML發現,頁
面有多個
<input name="dg1$ctl02$TextBox1" type="text" id="dg1_ctl02_TextBox1" /> <input name="dg1$ctl03$TextBox1" type="text" id="dg1_ctl03_TextBox1" />TextBox1隱藏了,給DataGrid添加選擇列,通過以下方法獲得被選擇行的TextBox1 protected void dg1_SelectedIndexChanged(object sender, EventArgs e) { Control c = this.dg1.Items[this.dg1.SelectedIndex].FindControl("TextBox1"); //Control c = this.dg1.SelectedItem.FindControl("TextBox1"); TextBox tb = (TextBox)c; tb.Text = "TextBox"; } protected void dg1_EditCommand(object source, DataGridCommandEventArgs e) { TextBox tb = (TextBox)e.Item.FindControl("TextBox1"); this.Label1.Text = tb.Text.ToString(); }
如果是在DataGrid的頁首和頁尾:
((TextBox)this.dg1.Controls[0].Controls[0].FindControl("TextBoxH")).Text = "Head"; ((TextBox)this.dg1.Controls[0].Controls[this.dg1.Controls[0].Controls.Count -1].FindControl("TextBoxF")).Text = "Footer";TextBox1在Repeater中 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"OnItemCommand="Repeater1_ItemCommand"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox><%#DataBinder.Eval(Container.DataItem,"ProductName")%><asp:Button ID="btn"OnClick="btn_click" runat="server" Text="dddd" /><br /> </ItemTemplate> </asp:Repeater>
通過按鈕來獲得TextBox1:
protected void btn_click(object sender, EventArgs e) { //獲得按鈕 Button btn = (Button)sender; TextBox tb = (TextBox)btn.Parent.FindControl("TextBox1"); tb.Text = "Text"; }
或者
foreach (RepeaterItem item in this.Repeater1.Items) { ((TextBox)item.FindControl("TextBox1")).Text = "Text2"; }
自訂控制項裡的TextBox1
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
引用<uc1:WebUserControl ID="WebUserControl1" runat="server" />
擷取TextBox1:
((TextBox)this.WebUserControl1.FindControl("TextBox1")).Text = "userc";
模板頁訪問頁面TextBox1
//模板頁的TextBox1 TextBox tbM = (TextBox)this.FindControl("TextBox1"); //頁面中的TextBox1 TextBox tbC = (TextBox)this.FindControl("ContentPlaceHolder1").FindControl
("TextBox1"); tbC.Text = tbM.Text;
頁面使用模板頁的TextBox1
//模板頁的TextBox1 TextBox tbM = (TextBox)Master.FindControl("TextBox1"); //本頁面的TextBox1 //錯誤的方法:TextBox tbC = (TextBox)this.FindControl("TextBox1"); TextBox tbC = (TextBox)Master.FindControl
("ContentPlaceHolder1").FindControl("TextBox1"); tbM.Text = tbC.Text.ToString();
【參考】http://www.cnblogs.com/12go/archive/2011/11/27/2264853.html
FindControl的詳細介紹