我正在编写主页的代码,需要知道当前显示的子页面是哪个。如何通过程序实现这一功能?
我使用这个:
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
它返回这种格式的类名 "ASP.default_aspx",但我发现大多数情况下很容易解析。
希望能帮到你!
最好让ContentPage
通知MasterPage
。这就是为什么ContentPage
有一个Master
属性,而MasterPage
没有Child
属性的原因。
在这种情况下,最佳实践是在MasterPage
上定义一个属性或方法,并通过ContentPage
的Master
属性使用它。
如果您使用此技术,最好明确指定MasterPage
的类名。这使得在ContentPage
中使用MasterPage
更加方便。
例如:
//Page_Load
MyMaster m = (MyMaster)this.Master;
m.TellMasterWhoIAm(this);
希望这能有所帮助。Master.SomeProperty = "SomeValue";
。 - Theophilus我有一个理由要检查主页面中的子页面。
我的所有菜单选项都在主页面上,如果某些系统设置未设置,则需要禁用它们。
如果没有设置,则会显示一条消息并禁用按钮。由于设置页面是从此主页面获得的内容页面,因此我不希望在所有设置页面上持续显示该消息。
这段代码对我很有用:
//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("_",".")
以下是我对这个问题的解决方案(这段代码放在主页面的代码后面):
if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
// your code here
}
if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
// your code here
}
Request.CurrentExecutionFilePath;
或者
Request.AppRelativeCurrentExecutionFilePath;
string s = Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
if (s == "default.aspx")
{ /* do something */ }
以下代码运行得非常好,试一试吧
string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
string pageName = this.ContentPlaceHolder1.Page.GetType().BaseType.Name;
这个语句来获取子页面的基类类型。在大多数情况下,它会返回不带 "aspx" 标签的页面地址。例如,“Default”。 - Tarık Özgün Güner