asp.net placeholder, panel and other controls do not implement INamingContainer, resulting in FindControl invalid _ practical skills

Source: Internet
Author: User
The code is as follows:
Copy Code code as follows:

Panel spnbuttons = new Panel ();
Button btn = New button ();
Btn.id = "BTN1";
SPNBUTTONS.CONTROLS.ADD (BTN);
Output true to indicate that the control was not found
Response.Write (Spnbuttons.findcontrol (btn.id) = = null);

And if it's the following code, it's OK:
Copy Code code as follows:

Panel spnbuttons = new Panel ();
PAGE.CONTROLS.ADD (spnbuttons);//Create a panel and add it to the page

Button btn = New button ();
Btn.id = "BTN1";
SPNBUTTONS.CONTROLS.ADD (BTN);
Output false to indicate that the control was found
Response.Write (Spnbuttons.findcontrol (btn.id) = = null);

Or you can use repeater:
Copy Code code as follows:

Repeater spnbuttons = new Repeater ();

Button btn = New button ();
Btn.id = "BTN1";
SPNBUTTONS.CONTROLS.ADD (BTN);
Output false to indicate that the control was found
Response.Write (Spnbuttons.findcontrol (btn.id) = = null);

Check it out. The panel is inherited from WebControl, and WebControl is defined as:
public class Webcontrol:control, IAttributeAccessor
{}
The definition of repeater is:
public class Repeater:control, INamingContainer
{}
Is it because repeater realized the reason for INamingContainer?
I have also customized a class that inherits from the panel and implements the INamingContainer, and can find the control:
Copy Code code as follows:

public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Mypanel spnbuttons = new Mypanel ();

Button btn = New button ();
Btn.id = "BTN1";
SPNBUTTONS.CONTROLS.ADD (BTN);

Response.Write (Spnbuttons.findcontrol (btn.id) = = null);
}


}

public class Mypanel:panel, INamingContainer
{
Public Mypanel (): Base ()
{
}
}

, ASP. NET, placeholder, panel and other controls do not implement INamingContainer, resulting in FindControl invalid
If you add these controls to the parent control that implements INamingContainer, or implement INamingContainer with subclasses, you can make FindControl effective.
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.