Using "FindControl" on the base page

My page, Default.aspx inherits the form BasePage.cs.

On the base page, I'm trying to find the Label1 control, which is actually located in Default.aspx.

var labelControl = (TextBox)Page.FindControl("Label1"); 

However, this always returns as null. Can I find controls on other pages through the base page?

+1
source share
3 answers

FindControl is not recursive (the ASP.NET team did not implement it for performance reasons).

+4
source

Set the ClientIDMode tags, and then try to find the tag in BasePage.cs

  protected override void OnLoad(EventArgs e) { Label lbl = this.FindControl("Label1") as Label; } 

Hope this helps.

0
source

If FindControl () does not work, it should be possible by declaring your controls as properties in your BasePage class. Assuming Default.aspx and other .aspx pages will inherit from BasePage, you should do this:

 public class BasePage { protected Label Label1; } 

In BasePage methods, check to see if your properties are null. If so, management exists and can be managed:

 protected void SomeBasePageMethod() { if (this.Label1 != null) { // Do something with Label1 } } 
0
source

All Articles