C# 如何确定从母版页显示哪个子页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/269050/
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-03 20:52:50  来源:igfitidea点击:

How to determine which Child Page is being displayed from Master Page?

c#asp.netmaster-pages

提问by jinsungy

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?

我正在母版页上编写代码,我需要知道正在显示哪个子(内容)页面。如何以编程方式执行此操作?

采纳答案by Gord

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.

这听起来是个坏主意。master 的想法是它不应该关心有哪个页面,因为这是每个页面的所有公共代码。

回答by JoshBerke

回答by wulimaster

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.

Page.Request.Url.PathAndQuery 或 Url Uri 对象的其他属性之一应该可以从母版页代码中使用。

回答by spilliton

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:

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

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 重写,我不确定这是否有效。

回答by Kon

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
}

回答by Norbert B.

It's better to let the ContentPagenotify the MasterPage. That's why the ContentPagehas a MasterProperty and MasterPagedoes not have Childproperty. Best pratice in this is to define a property or method on the MasterPageand use this through the Masterproperty of the ContentPage.

最好让ContentPage通知MasterPage。这就是为什么ContentPage拥有Master财产和MasterPage没有Child财产。最好的做法是在 上定义一个属性或方法,MasterPage并通过 的Master属性使用它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.

如果您使用此技术,最好明确指定 MasterPage 的类名。这使得在 ContentPage 中使用 MasterPage。

Example:

例子:

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

m.TellMasterWhoIAm(this);

Hope this helps.

希望这可以帮助。

回答by Todd H.

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!

希望有帮助!

回答by WraithNath

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

回答by Emad Mokhtar

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"))
{

}

回答by Sarim Shekhani

Use the Below code.

使用下面的代码。

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