如何确定母版页中正在显示哪个子页?
我正在母版页上编写代码,我需要知道正在显示哪个子(内容)页面。 我怎样才能以编程方式做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我使用这个:
它以“ASP.default_aspx”格式重新调整类名,但我发现对于大多数用途来说很容易解析。
希望有帮助!
I use this:
It retuns the class name in this format "ASP.default_aspx", but I find that easy to parse for most purposes.
Hope that helps!
最好让
ContentPage
通知MasterPage
。 这就是为什么ContentPage
有Master
属性而MasterPage
没有Child
属性。最佳实践是在
MasterPage
上定义一个属性或方法,并通过ContentPage
的Master
属性使用它。如果您使用此技术,最好显式指定母版页的类名。 这使得在内容页中使用母版页。
示例:
希望这会有所帮助。
It's better to let the
ContentPage
notify theMasterPage
. That's why theContentPage
has aMaster
Property andMasterPage
does not haveChild
property.Best pratice in this is to define a property or method on the
MasterPage
and use this through theMaster
property of theContentPage
.If you use this technique it's best to explicitly specify the classname for the MasterPage. This makes to use the MasterPage in the ContentPage.
Example:
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:
使用下面的代码。
Use the Below code.
您可以使用:
Request.CurrentExecutionFilePath
You can use:
Request.CurrentExecutionFilePath
这是我对问题的解决方案(此代码进入母版页后面的代码):
或者更复杂一点,但可读性较差:
Here is my solution to the problem (this code goes into the code behind the master page):
or a bit more sophisticated, but less readable:
或者
or
我在我的一个项目中做了类似的事情,根据正在加载的页面动态附加 css 文件。 我只是从请求中获取文件名:
然后从那里提取文件名。 我不确定如果您正在进行 URL 重写,这是否会起作用。
I do something similar to this in a project of mine to dynamically attach css files based on the page being loaded. I just get the name of the file from the request:
And then extract the file name from there. I'm not sure if this will work if you are doing URL re-writes though.
您可以通过获取最后一个段或请求来完成此操作,我将是表单名称
You can do this by getting the last segmant or the request and I'll be the Form name
你可以试试这个:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
将该代码放入
Site.Master
的title
标记中You can try this one:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
Put that code within the
title
tags of theSite.Master
我使用的这么多答案
可以很容易地排除任何单个页面,并且由于您比较字符串,您甚至可以为页面添加前缀,例如 except_pagetitle 并比较标题的子字符串。 我通常使用它来从某些我不想加载的功能中排除登录页面,例如会话超时和实时聊天。
so many answers I am using
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. I use this commonly to exclude log in pages from certain features I don't want to load like session timeouts and live chat.
下面的代码工作起来就像一个迷人的..尝试一下
Below code worked like a charmed ..try it
Page.Request.Url.PathAndQuery 或 Url Uri 对象的其他属性之一应该可以从母版页代码中使用。
Page.Request.Url.PathAndQuery or one of the other properties of the Url Uri object should be available to you from the master page code.
您可以在代码隐藏中检查页面类型:
You can check the page type in the code-behind: