Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用;FindControl";在基本页上_C#_Asp.net_Validation - Fatal编程技术网

C# 使用;FindControl";在基本页上

C# 使用;FindControl";在基本页上,c#,asp.net,validation,C#,Asp.net,Validation,我的页面Default.aspx继承表单BasePage.cs 在基本页面中,我试图找到实际位于Default.aspx中的控件标签1 var labelControl = (TextBox)Page.FindControl("Label1"); 但是,它总是返回为null。我可以通过基本页找到其他页面的控件吗?FindControl不是递归的(ASP.NET团队由于性能原因没有实现它) FindControl不是递归的(ASP.NET团队由于性能原因尚未实现它) 设置标签的ClientMod

我的页面Default.aspx继承表单BasePage.cs

在基本页面中,我试图找到实际位于Default.aspx中的控件标签1

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

但是,它总是返回为null。我可以通过基本页找到其他页面的控件吗?

FindControl不是递归的(ASP.NET团队由于性能原因没有实现它)

FindControl不是递归的(ASP.NET团队由于性能原因尚未实现它)

设置标签的ClientMode&然后尝试在BasePage.cs中查找标签

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


}

希望这会有所帮助

设置标签的ClientMode&然后尝试在BasePage.cs中查找标签

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


}
希望这会有所帮助

如果FindControl()无法正常工作,则可以将控件声明为BasePage类中的属性。假设Default.aspx和其他.aspx页面都将从BasePage继承,您应该能够执行以下操作:

public class BasePage
{
  protected Label Label1;

}
在BasePage方法中,检查属性是否不为null。如果是,则控件存在并且可以进行操作:

protected void SomeBasePageMethod()
{
  if (this.Label1 != null) 
  {
    // Do something with Label1
  }
}
如果FindControl()无法正常工作,则可以在BasePage类中将控件声明为属性。假设Default.aspx和其他.aspx页面都将从BasePage继承,您应该能够执行以下操作:

public class BasePage
{
  protected Label Label1;

}
在BasePage方法中,检查属性是否不为null。如果是,则控件存在并且可以进行操作:

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

Label1是在页面的根目录中还是在其他一些控制面板中?记住FindControl不是递归的。它位于面板中。。。每页上的面板都不同。控件可能位于每页上另一个面板的面板中。我如何使它成为通用的,这样它就可以在任何页面上找到Label1,无论它在哪里+标签1是在页面的根目录中还是在其他控制面板中?记住FindControl不是递归的。它位于面板中。。。每页上的面板都不同。控件可能位于每页上另一个面板的面板中。我如何使它成为通用的,这样它就可以在任何页面上找到Label1,无论它在哪里+一个伟大的捕获。