0

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?

2 Answers 2

1

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.

1
  • Anytime. FYI: There's also a Page property on user controls which works the same way, so from the opposite end of the lifecycle.
    – Joe Enos
    Commented Apr 2, 2013 at 15:22
0

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.<%
}
2
  • Will this work if i want to see the page to be loaded before its loaded?
    – Lappies
    Commented Apr 2, 2013 at 15:15
  • This will work because when the aspx code from the master is executed, the page has already been loaded. Look for "webforms load order" and you can have a better understanding of what is going on when you use a master page. Commented Apr 2, 2013 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.