The specified control cannot be found in the Page. FindControl method.

Source: Internet
Author: User

In the previous event calling method between user controls, Page is used. the FindControl method can be used as an example in the result. When I use the FindControl method on the website, an error occurs. The following error occurs: "The object is not referenced to the instance of the object". Generally, the object is not initialized, the solution is as follows:

From http://blog.csdn.net/mengkun2010

"

1. First, let's understand how FindControl works:

In ASP. NET 2.0 introduces the MasterPage mechanism. When MasterPage is used on the current Page, Page cannot be used for controls placed on content pages such as ContentPlaceholder1. what is the reason for FindControl?

MSDN interpretation of FindControl: Search for the server control with the specified id parameter in the current named container

Here is an article on the FindControl method and INamingContainers interface: http://www.odetocode.com/Articles/116.aspx

The FindControl method searches for the control corresponding to the specified ControlID in the current naming container. The naming container is an object that implements the INamingContainer interface.

You can add the Trace = Ture command to the page instruction to track the page output and view the control tree. In the control tree of a Page, the Page object must be a top-level naming container, but it is not necessarily the only naming container. For example, if a GridView exists, the GridView is actually a naming container. To find a ControlID in the GridView, you cannot use Page. FindControl, but you must use the [GridView object]. FindControl method.

Most of the time, because it is a dynamic control, it knows that it is in the same naming container, but it does not know what the naming container is. To find another control, you can use this. parent. findControl method.

2. Solve the problem:

Because after MasterPage is used, you can use Page again. findControl cannot be found. There is no doubt about this. Therefore, the correct method is to first find the naming container of this control, which is the ID of ContentPlaceHolder #, and then find your control, for example, Page. master. findControl ("ContentPlaceHolder1 "). findControl ("TextBox1 ").

3. Deep analysis:

Obviously, we solved the problem in step 2, but after careful analysis, it is not difficult to find that the solution itself has another problem, that is, if the ID of ContentPlaceHolder1 is changed, then your CODE cannot be used and you need to change it.

A better solution is:

TextBox [] tbx = new TextBox [] {this. TextBox1, this. TextBox2 ......}

Use tbx [index] as the control you want.

"

And a post http://topic.csdn.net/u/20100820/14/836ebf37-3989-4868-98c5-1795de7f5c73.html in csdn.

"

  • Gongshunkai
  • (Gongshunkai)
  • Level 1:
  • Post completion rate:100.00%
LandlordPosted on: 14:31:18
(LinkButton) Page. FindControl ("LinkButton" + I. ToString (). CssClass = "selectTag2323"; error: object reference is not set to the instance of the object

I checked it online:

For example, ASP. NET 2.0 introduces the MasterPage mechanism. When MasterPage is used on the current page, the current page also produces naming iner like ContentPlaceholder1. In this case, you can find the control of the current page, page cannot be used either. instead, use the ContentPlaceholder1.FindControl method.

Most of the time, because it is a dynamic control, it knows that it is in the same naming container, but it does not know what the naming container is. To find another control, you can use this. parent. findControl method.

My LinkButton is in <asp: Content ID = "Content2" ContentPlaceHolderID = "ContentPlaceHolder2" Runat = "Server">

I changed it to (LinkButton) ContentPlaceHolder2.FindControl ("LinkButton" + I. ToString (). CssClass = "selectTag2323"; error: the name "ContentPlaceHolder2" does not exist in the current context"

My page structure is as follows: mother board page-child mother board page-current page

How can this problem be solved?

  • Net_lover
  • (Chapter E of Mencius ])
  • Level 1:
  • 3

    3

    More medals
# 1st floor score: 0Reply: 14:42:39

Page. Master. Master. FindControl ("xxxx ").
  • Useful to me [0]
  • Drop bricks [0]
  • Reference
  • Report
  • Management
  • TOP
Excellent recommendation: Database Class Extension: database creation and table Creation
  • Wuyq11
  • (Life is like a dream)
  • Level 1:
  • 2

    20

    3

    More medals
# Second floor score: 0Reply: 14:58:58

Use this. Master to obtain the immediate Master, so that this. Master. Master can obtain the parent Master.
Master. Master. FindControl ("")
TextBox textBoxFind = (TextBox) this. Page. Master. FindControl (masterPageContentPlaceHolderID). FindControl ("");
  • Net_lover
  • (Chapter E of Mencius ])
  • Level 1:
  • 3

    3

    More medals
#13 floor score: 0Reply: 20:47:29

Didn't you tell me? "? Page. Master. Master. FindControl, why don't you try it ??

LinkButton lb = Page. Master. Master. FindControl ("ContentPlaceHolder1"). FindControl ("ContentPlaceHolder2"). FindControl ("LinkButton1") as LinkButton;
Lb. Text = "yyyyy ";

  • Useful to me [0]
  • Drop bricks [0]
  • Reference
  • Report
  • Management
  • TOP
  • Net_lover
  • (Chapter E of Mencius ])
  • Level 1:
  • 3

    3

    More medals
# Floor 14 score: 0Reply: 20:52:10

The strict test process is as follows: For ease of creation, create a single file
1. parent. master file:
HTML code
<% @ Master Language = "C #" AutoEventWireup = "true" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2, child. master

HTML code
<% @ Master Language = "C #" AutoEventWireup = "true" MasterPageFile = "~ /Parent. master "%> <asp: Content ID =" Content1 "ContentPlaceHolderID =" ContentPlaceHolder1 "Runat =" Server ">

3. Content Page

HTML code
<% @ Page Language = "C #" AutoEventWireup = "true" MasterPageFile = "~ /Child. master "%> <script runat =" server "> protected void Page_Load (object sender, EventArgs e) {for (int I = 1; I <6; I ++) {LinkButton lb = Page. master. master. findControl ("ContentPlaceHolder1 "). findControl ("ContentPlaceHolder2 "). findControl ("LinkButton" + I. toString () as LinkButton; lb. text = "New Value" + I. toString () ;}</script> <asp: content ID = "Content2" ContentPlaceHolderID = "ContentPlaceHolder2" runat = "Server">

"

Meng Ye is angry ..

My website uses a master page, so I should write it like this and find it step by step following the element name.

BJshitishu tname = Page. Master. FindControl ("MainContent"). FindControl ("BJshitishu1") as BJshitishu;

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.