Lappies
Lappies

Reputation: 923

Using master page and web forms in C#

When using a master page, is there anyway i can see what web form is currently loaded or being loaded in the master page, so before this line:

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

i would like to see/get the web form that's going to be loaded in the master page, is this possible?

Upvotes: 0

Views: 837

Answers (2)

Joe Enos
Joe Enos

Reputation: 40403

Yes, you have access to the Page property from the master page.

To see this in action, from the default Visual Studio template of an ASP.NET web application (which comes with a master page), dump this in the master where the h1 title is:

My ASP.NET Application <%= Page.GetType().Name %>

It will show up in the browser as My ASP.NET Application default_aspx or whatever page you're on.

Upvotes: 1

Ricardo Souza
Ricardo Souza

Reputation: 16456

I don't know if you have access to the page being loaded, but you can create a public property on the master page called eg currentPage and set it on the Page_Load of the page itself to the value you want. This way you can check it on the Site.Master code:

// on the Site.Master.cs
public string CurrentPage;


// on the page, inside the Page_Load event
((Site)this.Master).CurrentPage = 'My page';


// on the Site.Master
<%
if (CurrentPage == "My page")
{
    %>My page is loaded.<%
}
else
{
    %>Another page is loaded.<%
}

Upvotes: 0

Related Questions