Find a control on a page with a master page
Asked Answered
N

3

19

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

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.

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

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

How can I access the Table?

Nun answered 15/9, 2010 at 18:25 Comment(0)
T
38

Try this

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

Checkout this Control ID Naming in Content Pages for more details

Tribunal answered 15/9, 2010 at 18:38 Comment(0)
A
1

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.

Ancilin answered 15/9, 2010 at 18:36 Comment(0)
I
1

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.

you should add this line in child page:

<%@ MasterType VirtualPath="~/MasterPage.master" %>
Insert answered 18/9, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.