在具有母版页的页面上找到控件

时间:2021-03-11 15:56:27

I have to find a Control in an aspx page bound to a master page.

我必须在一个与母版页绑定的aspx页面中找到一个控件。

The master page contains:

主页包含:

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

The content page contains:

页面包含的内容:

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

I added a Table with ID formtable as a child of Content2.

我添加了一个ID formtable作为Content2的子元素的表。

I tried to use the following code to access the Table, but the code returns null:

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

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

How can I access the Table?

我怎样才能进入这张桌子?

3 个解决方案

#1


26  

Try this

试试这个

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

Checkout this Control ID Naming in Content Pages for more details

在内容页中签出此控件ID命名以获得更多详细信息

#2


0  

What context are you in when you are trying to do this? Are you in the codebehind of the individual page?

当你尝试这样做的时候,你处于什么环境中?你在个人页面的代码后面吗?

If you are it should be Content1.FindControl("formtable") as Table and that would be it.

如果是,它应该是Content1.FindControl("formtable")作为表,就是这样。

#3


0  

Working with findControl() cause complications sometimes. It is easier to define a public property for that control in master page and then access control through the property.

使用findControl()工作有时会导致并发症。在主页面中定义该控件的公共属性,然后通过属性访问控制更容易。

you should add this line in child page:

您应该在子页面中添加这一行:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

#1


26  

Try this

试试这个

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

Checkout this Control ID Naming in Content Pages for more details

在内容页中签出此控件ID命名以获得更多详细信息

#2


0  

What context are you in when you are trying to do this? Are you in the codebehind of the individual page?

当你尝试这样做的时候,你处于什么环境中?你在个人页面的代码后面吗?

If you are it should be Content1.FindControl("formtable") as Table and that would be it.

如果是,它应该是Content1.FindControl("formtable")作为表,就是这样。

#3


0  

Working with findControl() cause complications sometimes. It is easier to define a public property for that control in master page and then access control through the property.

使用findControl()工作有时会导致并发症。在主页面中定义该控件的公共属性,然后通过属性访问控制更容易。

you should add this line in child page:

您应该在子页面中添加这一行:

<%@ MasterType VirtualPath="~/MasterPage.master" %>