The FindControl method is very similar to getElementById (string) in JavaScript.
How to use FindControl Control.findcontrol (String): Search in the current naming container with the specified ID
The server control for the parameter.
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>
As in the previous code, we would like to locate the TextBox1 can be used page.control[0]. FindControl ("Form1"). FindControl ("TextBox1") ...
Or it can be
protected void Button1_Click (object sender, EventArgs e)
{
Control C = this. FindControl ("TextBox1");
TextBox tb= (textbox) C;
FindControl returns a control type that requires a forced type to be converted to a TextBox type
TextBox tb= (textbox) this. FindControl ("TextBox1");
This. Label1.Text = tb. Text;
}
When the TextBox1 is placed in another control:
<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>
When TextBox1 in the panel, it doesn't seem to have any effect on the TextBox tb= (textbox) this. FindControl
("TextBox1"), when viewing the live page of the HTML code is found, the textbox ID has not changed, so you can get
TextBox1.
When the TextBox1 is placed in the 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 C Ommandname="Select"text="Select"></asp:ButtonColumn> </Columns> </asp:DataGrid>
This time. FindControl ("TextBox1") ==null, unable to get TextBox1, view build page HTML discovery, page
Polygon has multiple
<input name="Dg1$ctl02$textbox1"Type="text"Id="Dg1_ctl02_textbox1"/> <input name="Dg1$ctl03$textbox1"Type="text"Id="Dg1_ctl03_textbox1"/>TextBox1 is hidden, add a selection column to the DataGrid and get the TextBox1 of the selected row in the following waysprotected voidDg1_selectedindexchanged (Objectsender, 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 voidDg1_editcommand (Objectsource, DataGridCommandEventArgs E) {TextBox TB= (TextBox) E.item.findcontrol ("TextBox1"); This. Label1.Text =TB. Text.tostring (); }
If the header and footer are in the 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 in the 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>
To get TextBox1 by buttons:
protected void Btn_click (object sender, EventArgs e) {//Get button buttons btn = (button) sender; TextBox TB = (textbox) btn. Parent.findcontrol ("TextBox1"); Tb. Text = "text"; }
Or
foreach (RepeaterItem item in this.) Repeater1.items) {(TextBox) item. FindControl ("TextBox1")). Text = "Text2"; }
TextBox1 in custom controls
<%@ Control language= "C #" autoeventwireup= "true" codefile= "WebUserControl.ascx.cs"
inherits= "Webusercontrol"%> <asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
Reference <uc1:webusercontrol id= "WebUserControl1" runat= "Server"/>
Get TextBox1:
(TextBox) this. Webusercontrol1.findcontrol ("TextBox1")). Text = "UserC";
Template page access page TextBox1
Template page of the TextBox1 TextBox TbM = (textbox) this. FindControl ("TextBox1"); TextBox1 TextBox TbC = (textbox) This in the page. FindControl ("ContentPlaceHolder1"). FindControl
("TextBox1"); Tbc.text = Tbm.text;
Page TextBox1 using the template page
Template page of the TextBox1 TextBox TbM = (textbox) Master.findcontrol ("TextBox1"); This page TextBox1//Error method: TextBox TbC = (textbox) this. FindControl ("TextBox1"); TextBox TbC = (textbox) Master.findcontrol
("ContentPlaceHolder1"). FindControl ("TextBox1"); Tbm.text = TbC.Text.ToString ();
"Reference" http://www.cnblogs.com/12go/archive/2011/11/27/2264853.html
A detailed introduction to FindControl