MasterPage 是否知道正在显示哪个页面?

发布于 2024-07-07 18:22:18 字数 864 浏览 7 评论 0原文

当我使用 MasterPages 在网站上导航时,应用程序是否知道我所在的页面? 如果是这样,它是否将其存储在我可以访问的对象中?

我问的原因是这样我可以替换它:

//masterpage 
<div id="nav_main">
   <ul><asp:ContentPlaceHolder ID="navigation" runat="server">                    
   </asp:ContentPlaceHolder></ul>
</div>

//content page(s)
<asp:Content ContentPlaceHolderID="navigation" ID="theNav" runat="server">
   <li><a href="default.aspx">Home</a></li>
   <li id="current"><a href="faq.aspx">FAQ</a></li>
   <li><a href="videos.aspx">Videos</a></li>
   <li><a href="#">Button 4</a></li>
   <li><a href="#">Button 5</a></li>
</asp:Content>

使用更优雅的导航解决方案,通过将列表项的 ID 设置为“当前”来突出显示页面的链接。 当前,每个页面都会重新创建导航,并将其各自的链接 ID 设置为当前。

When I navigate on a website utilizing MasterPages, does the application know what page I am on? If so, does it store it in an object I can access?

The reason I am asking is so I can replace this:

//masterpage 
<div id="nav_main">
   <ul><asp:ContentPlaceHolder ID="navigation" runat="server">                    
   </asp:ContentPlaceHolder></ul>
</div>

//content page(s)
<asp:Content ContentPlaceHolderID="navigation" ID="theNav" runat="server">
   <li><a href="default.aspx">Home</a></li>
   <li id="current"><a href="faq.aspx">FAQ</a></li>
   <li><a href="videos.aspx">Videos</a></li>
   <li><a href="#">Button 4</a></li>
   <li><a href="#">Button 5</a></li>
</asp:Content>

With a more elegant solution for the navigation, which highlights the link to the page by having the list item's ID set to "current". Currently each page recreates the navigation with its respective link's ID set to current.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

北渚 2024-07-14 18:22:18

我同意克里斯的观点:使用一个控件来处理此菜单的显示,并使其知道应突出显示哪些链接。 这是我经常使用的方法。 如果您有多个页面需要不同样式的相同链接,那么情况可能会变得更加复杂,但您已经明白了。

Dim thisURL As String = Request.Url.Segments(Request.Url.Segments.Count - 1)
Select Cast thisUrl
   Case "MenuItem1.aspx"
       lnkMenu1.CssClass = "Current"
   Case "MenuItem2.aspx"
       lnkMenu2.CssClass = "Current"
End Select

I'd concur with Chris: use a control to handle display of this menu and make it aware of what link should be highlighted. Here's a method I use regularly. It may become more complex if you've got multiple pages that would need the same link styled differently, but you get the idea.

Dim thisURL As String = Request.Url.Segments(Request.Url.Segments.Count - 1)
Select Cast thisUrl
   Case "MenuItem1.aspx"
       lnkMenu1.CssClass = "Current"
   Case "MenuItem2.aspx"
       lnkMenu2.CssClass = "Current"
End Select
誰ツ都不明白 2024-07-14 18:22:18

要从母版页中获取当前请求 URL,您可以执行以下操作:

string s = this.Page.Request.FilePath; // "/Default.aspx"

我还建议将导航移至母版页而不是内容页。 这将使维护/访问变得更容易。

To get the current request URL from within the master page you would do:

string s = this.Page.Request.FilePath; // "/Default.aspx"

I also recommend moving your navigation into the master page instead of the content page. This will make it easier to maintain / access.

蔚蓝源自深海 2024-07-14 18:22:18

是的,在您的主文件中使用以下代码。 它将为您提供内容页面名称。

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

Yes, Use the below code in your master file. It will give you the content page name.

Page.ToString().Replace("ASP.","").Replace("_",".")
远山浅 2024-07-14 18:22:18

或者,如果您为子页面设置了特定标题而不是母版页,则可以搜索页面标题尝试:

this.Page.Title

希望有帮助。

Alternatively you can search for page title if you have set an specific title to the child page instead of masterpage try:

this.Page.Title

Hope it helps.

情归归情 2024-07-14 18:22:18

这是在 C# 中

string thisURL = Request.Url.Segments[Request.Url.Segments.Length - 1];
        if (thisURL.ToLower()== "default.aspx") li1.Attributes.Add("class","yekan active");
        if (thisURL.ToLower() == "experts.aspx") li2.Attributes.Add("class", "yekan active");

this is in C#

string thisURL = Request.Url.Segments[Request.Url.Segments.Length - 1];
        if (thisURL.ToLower()== "default.aspx") li1.Attributes.Add("class","yekan active");
        if (thisURL.ToLower() == "experts.aspx") li2.Attributes.Add("class", "yekan active");
聊慰 2024-07-14 18:22:18

您应该能够通过访问 Page 属性来获取该页面。 IE:

string type = this.Page.GetType().Name.ToString();

You should be able to get the page by accessing the Page property. IE:

string type = this.Page.GetType().Name.ToString();
眸中客 2024-07-14 18:22:18

您可能只需使用 请求路径 之一设置当前的母版页。 我可能还会在母版页上有一个属性来覆盖它,以便没有链接或其他内容的页面可以将其设置为合理的值。

You'd probably just use one of the Request path from within the master page to set the current. I'd probably also have a property on the master page to override it, so that pages without links or something could set it to something reasonable.

仅此而已 2024-07-14 18:22:18

它以这种方式对我有用 - 谢谢 Jared

这就是我所做的,让我们的导航菜单突出显示我们正在查看的页面的当前菜单项。 代码位于母版页中。
你基本上得到了文件路径(贾里德的方式)
我们在链接中使用“~”,所以我不得不将其删除。
迭代 Menu 控件的 menuItems 集合。
比较 navigatorUrl 属性。

(我不是最好的编码员,更不擅长解释 - 但它确实有效,而且我对此非常满意!)

protected void HighlightSelectedMenuItem()
    {
        string s = this.Page.Request.FilePath; // "/Default.aspx"
        string nav;
        if (s.Contains("~"))
        {
            s = s.Remove(s.IndexOf("~"), 1);
        }

        foreach (MenuItem item in navMenu.Items)
        {
            if (item.NavigateUrl.Contains("~"))
            {
                nav = item.NavigateUrl.Remove(item.NavigateUrl.IndexOf("~"), 1);
                if (s == nav)
                {
                    item.Selected = true;
                }
            }

        }
    }

It worked for me this way - Thanks Jared

This is what I did to get our nav menu to highlight the current menu item for the page that we are viewing. The code is in the master page.
You basically get the filepath (Jared's way)
We use the "~" in our links so I had to strip that out.
Iterate the menuItems collection of the Menu control.
Compare the navigateUrl property.

(I'm not the best coder and even worse at explaining - but it works and I was quite chuffed with it!)

protected void HighlightSelectedMenuItem()
    {
        string s = this.Page.Request.FilePath; // "/Default.aspx"
        string nav;
        if (s.Contains("~"))
        {
            s = s.Remove(s.IndexOf("~"), 1);
        }

        foreach (MenuItem item in navMenu.Items)
        {
            if (item.NavigateUrl.Contains("~"))
            {
                nav = item.NavigateUrl.Remove(item.NavigateUrl.IndexOf("~"), 1);
                if (s == nav)
                {
                    item.Selected = true;
                }
            }

        }
    }
单调的奢华 2024-07-14 18:22:18
string s = this.Page.GetType().FullName;
string[] array = s.Split('_');
int count = array.Count<String>();
string currentPage = array[count - 2];
string s = this.Page.GetType().FullName;
string[] array = s.Split('_');
int count = array.Count<String>();
string currentPage = array[count - 2];
愿得七秒忆 2024-07-14 18:22:18

导航控件(而不是母版页)应该负责当前突出显示的页面。

加载的页面应该通知导航项是谁,或者导航控件本身应该跟踪它。

关键是母版页应该只是显示内容的容器。它们不应该控制任何东西。

The navigation control, not the master page, should be in charge of what page is currently highlighted.

Either the page that is loaded should notify the navigation item who it is, or the nav control itself should keep track of it.

The point is that master pages are supposed to simply be a holder that content is displayed in. They aren't supposed to control anything.

_失温 2024-07-14 18:22:18

尝试

this.Page.Master

它会给你当前页面的母版页。

try

this.Page.Master

It will get you the master page of the current page.

折戟 2024-07-14 18:22:18

还有Request.RawURL

There's also the Request.RawURL

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