如何确定母版页中正在显示哪个子页?

发布于 2024-07-08 03:39:43 字数 56 浏览 5 评论 0原文

我正在母版页上编写代码,我需要知道正在显示哪个子(内容)页面。 我怎样才能以编程方式做到这一点?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

屋顶上的小猫咪 2024-07-15 03:39:43

我使用这个:

string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;

它以“ASP.default_aspx”格式重新调整类名,但我发现对于大多数用途来说很容易解析。

希望有帮助!

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.

Hope that helps!

酒绊 2024-07-15 03:39:43

最好让 ContentPage 通知 MasterPage。 这就是为什么 ContentPageMaster 属性而 MasterPage 没有 Child 属性。
最佳实践是在 MasterPage 上定义一个属性或方法,并通过 ContentPageMaster 属性使用它。

如果您使用此技术,最好显式指定母版页的类名。 这使得在内容页中使用母版页。

示例:

//Page_Load
MyMaster m = (MyMaster)this.Master;

m.TellMasterWhoIAm(this);

希望这会有所帮助。

It's better to let the ContentPage notify the MasterPage. That's why the ContentPage has a Master Property and MasterPage does not have Child property.
Best pratice in this is to define a property or method on the MasterPage and use this through the Master property of the ContentPage.

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:

//Page_Load
MyMaster m = (MyMaster)this.Master;

m.TellMasterWhoIAm(this);

Hope this helps.

挖个坑埋了你 2024-07-15 03:39:43

这听起来像是一个坏主意。 主机的想法是,它不应该关心那里有什么页面,因为这是每个页面的所有公共代码。

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.

戏剧牡丹亭 2024-07-15 03:39:43

我有理由检查母版页中的子页面。

我的主页上有所有菜单选项,如果未设置某些系统设置,则需要禁用它们。

如果不是,则会显示一条消息并且按钮被禁用。 由于设置页面是该母版页的内容页面,因此我不希望该消息继续显示在所有设置页面上。

这段代码对我有用:

                //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");
                }

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");
                }
腹黑女流氓 2024-07-15 03:39:43

使用下面的代码。

Page.ToString().Replace("ASP.","").Replace("_",".")

Use the Below code.

Page.ToString().Replace("ASP.","").Replace("_",".")
提赋 2024-07-15 03:39:43

这是我对问题的解决方案(此代码进入母版页后面的代码):

if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
   // your code here
}

或者更复杂一点,但可读性较差:

if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
   // your code here
}

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
}
伴我心暖 2024-07-15 03:39:43
Request.CurrentExecutionFilePath;

或者

Request.AppRelativeCurrentExecutionFilePath;
Request.CurrentExecutionFilePath;

or

Request.AppRelativeCurrentExecutionFilePath;
深白境迁sunset 2024-07-15 03:39:43

我在我的一个项目中做了类似的事情,根据正在加载的页面动态附加 css 文件。 我只是从请求中获取文件名:

this.Request.Url.AbsolutePath

然后从那里提取文件名。 我不确定如果您正在进行 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:

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.

长亭外,古道边 2024-07-15 03:39:43

您可以通过获取最后一个段或请求来完成此操作,我将是表单名称

string pageName = this.Request.Url.Segments.Last(); 

if (pageName.Contains("EmployeeTermination.aspx"))
{

}

You can do this by getting the last segmant or the request and I'll be the Form name

string pageName = this.Request.Url.Segments.Last(); 

if (pageName.Contains("EmployeeTermination.aspx"))
{

}
月牙弯弯 2024-07-15 03:39:43

你可以试试这个:


<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>

将该代码放入 Site.Mastertitle 标记中

You can try this one:


<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>

Put that code within the title tags of the Site.Master

孤凫 2024-07-15 03:39:43
string s =   Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
        if (s == "default.aspx")
              { /* do something */ }
string s =   Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
        if (s == "default.aspx")
              { /* do something */ }
白云不回头 2024-07-15 03:39:43

我使用的这么多答案

<%if(this.MainContent.Page.Title != "mypagetitle") { %>
<%}%>

可以很容易地排除任何单个页面,并且由于您比较字符串,您甚至可以为页面添加前缀,例如 except_pagetitle 并比较标题的子字符串。 我通常使用它来从某些我不想加载的功能中排除登录页面,例如会话超时和实时聊天。

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. I use this commonly to exclude log in pages from certain features I don't want to load like session timeouts and live chat.

风轻花落早 2024-07-15 03:39:43

下面的代码工作起来就像一个迷人的..尝试一下

string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];

Below code worked like a charmed ..try it

string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
聽兲甴掵 2024-07-15 03:39:43

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.

老街孤人 2024-07-15 03:39:43

您可以在代码隐藏中检查页面类型:

// 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文