C# 母版页——获取调用页面名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/967271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:25:55  来源:igfitidea点击:

Master pages -- Getting calling page name

c#asp.netvb.netmaster-pages

提问by Icemanind

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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)?

我创建了一个名为 Test.aspx 的页面,它继承了这个母版页,我怎么能从母版页知道 Test.aspx 已经被请求(因为这个母版页可以被 1 个以上的 aspx 页面继承)?

采纳答案by Zhaph - Ben Duguid

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.

1) 在您的登录页面中,在非回发时,检查 Referrer - 这将是将您发送到登录页面 ( Request.UrlReferrer) 的页面,将其存储在会话中以回发他们的登录详细信息,然后发送用户回来。

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

2) 使用ASP.NET标准功能来处理登录/重定向(基本上使用与 1 相同的系统)。

回答by spmason

Just use the "Page" member of the masterpage

只需使用母版页的“页面”成员

回答by Brandon

This is a similar questionto 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.

您所问的问题类似,但我会注意已接受的答案,试图从母版页找出子页面可能不是一个好的设计理念。

回答by John Sheehan

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:

在您的 .aspx 中添加此指令:

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

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

然后在您的 .aspx.cs 中设置属性值,如下所示:

Master.PageSection = "about";

回答by ichiban

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

从 MasterPage 中的代码隐藏中,该Page.AppRelativeVirtualPath属性将告诉您路径。