Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a control on a page with a master page

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?

like image 730
Aladdin Gallas Avatar asked Sep 15 '10 18:09

Aladdin Gallas


People also ask

Which control is available on the master page?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls.

How do I find the Div of a master page in the content page?

add to the div in the master page the attribute: runat="server" and then in the content page write the following code to access the div: Control c= this. Master. FindControl("masterDiv");// "masterDiv"= the Id of the div.

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.


1 Answers

Try this

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

Checkout this Control ID Naming in Content Pages for more details

like image 197
Vinay B R Avatar answered Oct 12 '22 00:10

Vinay B R