[英]How to determine which Child Page is being displayed from Master Page?
I'm writing code on the master page, and I need to know which child (content) page is being displayed. 我正在母版页上编写代码,我需要知道正在显示哪个子(内容)页面。 How can I do this programmatically? 我该如何以编程方式执行此操作?
I use this: 我用这个:
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
It retuns the class name in this format "ASP.default_aspx", but I find that easy to parse for most purposes. 它以这种格式“ASP.default_aspx”返回类名,但我发现很容易为大多数目的解析。
Hope that helps! 希望有所帮助!
It's better to let the ContentPage
notify the MasterPage
. 最好让ContentPage
通知MasterPage
。 That's why the ContentPage
has a Master
Property and MasterPage
does not have Child
property. 这就是为什么ContentPage
有一个Master
属性和MasterPage
没有Child
属性。 Best pratice in this is to define a property or method on the MasterPage
and use this through the Master
property of the ContentPage
. 最好的方法是在MasterPage
上定义属性或方法,并通过ContentPage
的Master
属性使用它。
If you use this technique it's best to explicitly specify the classname for the MasterPage. 如果使用此技术,最好明确指定MasterPage的类名。 This makes to use the MasterPage in the ContentPage. 这使得在ContentPage中使用MasterPage。
Example: 例:
//Page_Load
MyMaster m = (MyMaster)this.Master;
m.TellMasterWhoIAm(this);
Hope this helps. 希望这可以帮助。
This sounds like a bad idea to start with. 这听起来像个坏主意。 The idea of the master is that it shouldn't care what page is there as this is all common code for each page. 主人的想法是它不应该关心哪个页面,因为这是每个页面的所有常用代码。
I have had a reason to check the child page in the master page. 我有理由检查母版页中的子页面。
I have all my menu options on my master page and they need to be disabled if certain system settings are not set up. 我的母版页上有所有菜单选项,如果未设置某些系统设置,则需要禁用它们。
If they are not then a message is displayed and the buttons are disabled. 如果不是,则显示消息并禁用按钮。 As the settings page is a content page from this master page I don't want the message to keep being displayed on all the settings pages. 由于设置页面是来自此母版页的内容页面,因此我不希望该消息继续显示在所有设置页面上。
this code worked for me: 这段代码对我有用:
//Only show the message if on the dashboard (first page after login)
if (this.ContentPlaceHolder1.Page is Dashboard)
{
//Show modal message box
mmb.Show("Warning Message");
}
使用下面的代码。
Page.ToString().Replace("ASP.","").Replace("_",".")
You can use: 您可以使用:
Request.CurrentExecutionFilePath Request.CurrentExecutionFilePath
Here is my solution to the problem (this code goes into the code behind the master page): 这是我对问题的解决方案(此代码进入母版页后面的代码):
if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
// your code here
}
or a bit more sophisticated, but less readable: 或者更复杂,但不太可读:
if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
// your code here
}
Request.CurrentExecutionFilePath;
要么
Request.AppRelativeCurrentExecutionFilePath;
You can do this by getting the last segmant or the request and I'll be the Form name 您可以通过获取最后一个segmant或请求来完成此操作,我将成为表单名称
string pageName = this.Request.Url.Segments.Last();
if (pageName.Contains("EmployeeTermination.aspx"))
{
}
You can try this one: 你可以尝试这个:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
Put that code within the title
tags of the Site.Master
将该代码放在Site.Master
的title
标记内
I do something similar to this in a project of mine to dynamically attach css files based on the page being loaded. 我在我的一个项目中做了类似的事情,根据正在加载的页面动态附加css文件。 I just get the name of the file from the request: 我只是从请求中获取文件的名称:
this.Request.Url.AbsolutePath
And then extract the file name from there. 然后从那里提取文件名。 I'm not sure if this will work if you are doing URL re-writes though. 如果您正在进行URL重写,我不确定这是否有效。
string s = Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
if (s == "default.aspx")
{ /* do something */ }
so many answers I am using 我正在使用这么多答案
<%if(this.MainContent.Page.Title != "mypagetitle") { %>
<%}%>
this makes it easy to exclude any single page and since your comparing a string you could even prefix pages like exclude_pagetitle and comparing a sub-string of the title. 这样可以轻松排除任何单个页面,因为您比较字符串甚至可以为exclude_pagetitle等页面添加前缀,并比较标题的子字符串。 I use this commonly to exclude log in pages from certain features I don't want to load like session timeouts and live chat. 我通常使用它来从我不想加载的某些功能中排除登录页面,如会话超时和实时聊天。
下面的代码工作就像一个迷人的..它
string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
您应该可以从母版页代码中使用Page.Request.Url.PathAndQuery或Url Uri对象的其他属性之一。
You can check the page type in the code-behind: 您可以在代码隐藏中检查页面类型:
// Assuming MyPage1, MyPage2, and MyPage3 are the class names in your aspx.cs files:
if (this.Page is MyPage1)
{
// do MyPage1 specific stuff
}
else if (this.Page is MyPage2)
{
// do MyPage2 specific stuff
}
else if (this.Page is MyPage3)
{
// do MyPage3 specific stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.