c# - 在具有母版页的页面上查找控件

标签 c# asp.net findcontrol master-pages

我必须在绑定(bind)到母版页的 aspx 页面中找到一个 Control

母版页包含:

<asp:ContentPlaceHolder ID="MainContent" runat="server"/>               

内容页包含:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
</asp:Content>

我添加了一个带有 ID formtableTable 作为 Content2 的子项。

我尝试使用以下代码访问Table,但代码返回null:

protected void Ok_Click(object sender, EventArgs e)
{
    Table tblForm = this.FindControl("MainContent").FindControl("formtable") as Table;                 
}

如何访问

最佳答案

试试这个

Table tblForm = this.Master.FindControl("MainContent").FindControl("formtable") as Table; 

检查这个 Control ID Naming in Content Pages了解更多详情

关于c# - 在具有母版页的页面上查找控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3720590/

相关文章:

c# - ASP.NET 是否有更好的方法来查找其他控件内的控件?

c# - System.Timers.Timer 和 System.Threading.Timer 的不同行为

c# - 为单元测试设置类的只读属性

c# - 找到了 Automapper 未映射的成员 - 即使在我映射了这些成员之后

html - 如何放置一个asp :Literal underneath an asp:TextBox inside of a table

asp.net - 图像文件正在被另一个进程使用

asp.net - 使用 FindControl : Accessing Controls in a Formview

c# - Lazy<T> 与标准惰性实例化相比有哪些优势?

asp.net - MVC3 OutputCache VaryByHeader=Cookie 未设置

c# - gridview findcontrol 返回空 ""