开发者

Using "FindControl" on a base page

开发者 https://www.devze.com 2023-04-07 20:59 出处:网络
M开发者_运维问答y page, Default.aspx inherits form BasePage.cs. In the base page I am trying to find the control Label1 that is actually in Default.aspx.

M开发者_运维问答y page, Default.aspx inherits form BasePage.cs.

In the base page I am trying to find the control Label1 that is actually in Default.aspx.

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

However, this is always coming back as null. Can I find controls of other pages through the base page?


FindControl is not recursive (ASP.NET team haven't implemented it for performance reasons).


If FindControl() doesn't wind up working, it should be possible by declaring your controls as properties in your BasePage class. Assuming that Default.aspx and other .aspx pages will all inherit from BasePage, you should be able to do this:

public class BasePage
{
  protected Label Label1;

}

In BasePage methods, check if your properties are not null. If so, the control exists and can be manipulated:

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


Set the ClientIDMode of the Label & then try to find the label in BasePage.cs

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


}

Hope this will help.

0

精彩评论

暂无评论...
验证码 换一张
取 消