If you use FindControl ("id") to search for a control, the return value is Null. findcontrolnull
The function of searching for the page control by string ID and assigning values to the page control encountered the problem that the returned value of this. FindControl ("id") is Null. Record the solution.
The cause is that the parent control of the ID control I want to find is not caused by this.
So I wrote a recursive method to get the control:
1 /// <summary> 2 /// obtain a control on the page 3 /// </summary> 4 /// <param name = "control"> parent control container </param> 5 // <param name = "id"> Control ID </param> 6 /// <returns> </returns> 7 public Control GetControl (control Control, string id) 8 {9 Control con = control. findControl (id); 10 if (con = null) 11 {12 if (control. hasControls () 13 {14 foreach (Control c in control. controls) 15 {16 con = GetControl (c, id); 17 if (con = null) 18 continue; 19 else20 break; 21} 22} 23 else24 {25 return null; 26} 27} 28 return con; 29}
Usage: TextBox txt = GetControl (this, "textBox1") as TextBox; // you can find the TextBox Control whose ID is "textBox1" on the current page.
The following describes how to use FindControl to search for controls by ID on the ASPnet master page:
TextBox TextBoxSql = Master. FindControl ("ContentPlaceHolder1"). FindControl ("TextBox2" + n ["ID"]) as TextBox;
String title = TextBoxSql. Text. ToString ();
Change to this for a try:
String title = (TextBox) Master. ContentPlaceHolder1.FindControl ("TextBox2" + n ["ID"]). Text. ToString ();
Aspnet obtains the control by id and asks you how to solve the problem,
If it is in repeater:
For (int I = 0; I <Repeater4.Items. Count; I ++)
{
HiddenField h = Repeater4.Items [I]. FindControl ("HiddenField1") as HiddenField;
..
Js: document. getElementById (objID)