views:

1358

answers:

5

Can someone tell me how to get the name of the child page being called in a Master page scenario. For example, if I have the following masterpage:

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
</head>
<body>
    <asp:ContentPlaceHolder ID="cphMainContent" runat="server">
    </asp:ContentPlaceHolder>
</body>
</html>

And I create a page called Test.aspx which inherits this masterpage, how can I, from the masterpage, know that Test.aspx has been requested (since this masterpage can be inherited by more then 1 aspx page)?

A: 

Just use the "Page" member of the masterpage

spmason
+1  A: 

This is a similar question to what you're asking, but I would pay attention to the accepted answer, trying to figure out the child page from the master page is probably not a good design idea.

Brandon
Well actually, what I'm trying to do is not change anything on either pages. What I want to do in the master page is check for a session variable, which holds login info, and if the variable is not set, I wanted to redirect to a login page. I wanted to page a RefPage variable in my querystring though that holds the URL of the previous page, that way, after the user logs in, I can refer them back to the original page
icemanind
+1  A: 

I would notify in the other direction. Add properties to your master page then set them from the content pages.

In your master:

public partial class PortalMaster : System.Web.UI.MasterPage
{

    public string PageSection { get; set; }
}

In your .aspx add this directive:

<%@ MasterType VirtualPath="PortalMaster.master" %>

Then in your .aspx.cs set the property value like so:

Master.PageSection = "about";
John Sheehan
Well actually, what I'm trying to do is not change anything on either pages. What I want to do in the master page is check for a session variable, which holds login info, and if the variable is not set, I wanted to redirect to a login page. I wanted to page a RefPage variable in my querystring though that holds the URL of the previous page, that way, after the user logs in, I can refer them back to the original page
icemanind
On your log in page, check for Request.UrlReferrer. If it has a value (not null), store it in a hidden input, then redirect after log in. No querystring needed.
John Sheehan
A: 

From the codebehind in the MasterPage, the Page.AppRelativeVirtualPath property will tell you the path.

ichiban
+1  A: 

Going with your comments, I see a couple of options:

1) In your login page, on a non-postback, check the Referrer - this will be the page that sent you to the login page (Request.UrlReferrer), store that in session for the postback of their login details, and then send the user back.

2) Use the standard features of ASP.NET to handle login/redirections (which basically use the same system as 1).

Zhaph - Ben Duguid