Use FindControl (& quot; id & quot;) to find that the return value of the control is Null. findcontrolnull

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.