如何隐藏 .aspx 页面中的内容占位符?

发布于 2024-09-07 20:44:29 字数 358 浏览 0 评论 0原文

我的母版页有 3 个内容占位符:

1. left side bar

2. middle content area

3. right side bar

所以它看起来像:

<div id="left"></div>
<div id="content"></div>
<div id="right"></div>

在继承母版页的特定视图页面 (.aspx) 上,我现在想显示 #3(右侧栏)。

所以我根本不想渲染。

我该怎么做?

My master page has 3 content place holders:

1. left side bar

2. middle content area

3. right side bar

So it looks like:

<div id="left"></div>
<div id="content"></div>
<div id="right"></div>

On a particular view page (.aspx) that inherits the master page, I want to now show #3 (right side bar).

So I don't want the to be rendered at all.

How can I do this?

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

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

发布评论

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

评论(5

梦忆晨望 2024-09-14 20:44:29

另一件需要考虑的事情是嵌套母版页。我在当前项目中正在做类似的设计布局,并且我们有一个“基本”母版页,它包含所有脚本,包括页眉和页脚,并且只有一个 ContentPlaceHolder 代表页眉和页脚之间的所有内容。然后,我们有一个嵌套母版页,它使用基本母版页,但添加了右侧的“旁边”列。对于我们希望右列可见的页面,我们使用嵌套母版页。对于我们不希望呈现的页面,我们使用基本母版页。

像这样的策略肯定会阻止您的第 3 列被渲染(而不是被渲染并且只是空的,这可能无法实现您想要的布局)。

它看起来像这样:

基本母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterBase.Master.cs" Inherits="MasterBase" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    ...
    <asp:ContentPlaceHolder ID="head" runat="server" />
</head>

<body>
    <form id="form1" runat="server">
        <!-- HEADER -->
        ...

        <!-- CONTENT -->
        <asp:ContentPlaceHolder ID="bodyContent" runat="server" />

        <!-- FOOTER -->
        ...
    </form>
</body>
</html>

嵌套母版页

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server" />
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" runat="server">
    <!-- CENTER COLUMN -->
    <div id="centerCol">
        <asp:ContentPlaceHolder ID="bodyContent" runat="server" />
    </div>

    <!-- RIGHT COLUMN -->
    <div id="rightCol">
        <asp:ContentPlaceHolder ID="rightColumn" runat="server" />
    </div>
</asp:Content>

Another thing to consider is nested master pages. I have a similar design layout I'm doing in a current project, and we have a "base" master page that does all of our script includes, header and footer, and has just a single ContentPlaceHolder that represents everything between the header and footer. We then have a nested master page that uses the base master page, but adds a right "aside" column. For pages where we want the right column visible, we use the nested master page. For pages where we don't want it rendered, we use the base master page.

A strategy like this would definitely prevent your column #3 from being rendered at all (as opposed to being rendered and just being empty, which might not achieve the layout you're going for).

It looks something like this:

Base master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterBase.Master.cs" Inherits="MasterBase" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    ...
    <asp:ContentPlaceHolder ID="head" runat="server" />
</head>

<body>
    <form id="form1" runat="server">
        <!-- HEADER -->
        ...

        <!-- CONTENT -->
        <asp:ContentPlaceHolder ID="bodyContent" runat="server" />

        <!-- FOOTER -->
        ...
    </form>
</body>
</html>

Nested Master Page

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server" />
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" runat="server">
    <!-- CENTER COLUMN -->
    <div id="centerCol">
        <asp:ContentPlaceHolder ID="bodyContent" runat="server" />
    </div>

    <!-- RIGHT COLUMN -->
    <div id="rightCol">
        <asp:ContentPlaceHolder ID="rightColumn" runat="server" />
    </div>
</asp:Content>
稳稳的幸福 2024-09-14 20:44:29

有几种方法可以实现此目的,具体取决于“渲染”的含义。

一种方法是将 id 应用于每个页面的 body 标记,然后使用 CSS 隐藏您不想使用类似内容“渲染”的 div

#myPage #right{display:none;}

或者,如果“渲染”是指 HTML 响应中的输出,那么您可以用部分视图替换你的 div。然后,在运行时,您可以决定将哪些部分视图包含在页面中,作为所有控制器继承的基本控制器的一部分,或者作为每个控制器单独的一部分。

使用基本控制器是我复制一般代码的方法之一,其方式与您在 Web 表单中的母版页代码隐藏中所做的类似。

There's a few ways you can achieve this depending on what you mean by "render".

One way is to apply an id to each page's body tag and then use CSS to hide the divs you don't want to be 'rendered' using something like

#myPage #right{display:none;}

Alternatively, if by 'render' you mean output in the HTML response then you could replace your divs with a partial view. At run time you could then decide which partial views to include in your page, either as part of a base controller that all controllers inherit or as part of each controller individually.

Using a base controller is one of the ways I replicate general code in a similar way as you might have done in a master pages codebehind in webforms.

缘字诀 2024-09-14 20:44:29

最好使用内容占位符进行格式化并将实际内容放置在从 .master 页面继承的 .aspx 文件中。

这样,您就可以在母版中分配不带内容的 3 列 div,但将它们向左浮动,或者您希望使用 css 对其进行格式化,并使用 .aspx 中的内容引用将内容放入所需的列中。

附言。如果您希望创建 3 列布局,我建议使用 jquerys .ui-layout 插件而不是尝试假列等。

It would be best if you used the content place holders for formatting and placed the actual content inside the .aspx files that inherit from the .master page.

This way you assign your 3 column divs in you master without content but float them left or however you want to format them with your css and use the content reference in your .aspx to place the content into the desired column.

PS. if your looking to create 3 column layout I would suggest using jquerys .ui-layout plug-in instead of trying faux columns, etc..

明媚殇 2024-09-14 20:44:29

根据 System.Web.UI.WebControls,您可以使用 ContentPlaceHolder 类的 Visible 属性。

为此,您需要转到网页的 CodeBehind 文件。例如:

Child.aspx

<asp:ContentPlaceHolder id="HideMe" runat="server">
    ....
</asp:ContentPlaceHolder>

Child.aspx.vb

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    HideMe.Visible = false
End Sub

或者,正如 Jamie Dixon 所指出的,您还可以使用 css 规则 display:none; 隐藏 div

As per the documentation from System.Web.UI.WebControls, you can use the Visible property of the ContentPlaceHolder class.

To do this, you need to go to the CodeBehind file for the webpage. For example:

Child.aspx

<asp:ContentPlaceHolder id="HideMe" runat="server">
    ....
</asp:ContentPlaceHolder>

Child.aspx.vb

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    HideMe.Visible = false
End Sub

Alternatively, and as pointed out by Jamie Dixon, you can also hide the div with the css rule display:none;

似狗非友 2024-09-14 20:44:29

母版页

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  ....
</asp:ContentPlaceHolder>

<asp:Panel ID="PanelRightMenu" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
  ....
</asp:ContentPlaceHolder>
</asp:Panel>

子页代码绑定

this.Master.FindControl("PanelRightMenu").Visible = false;

Master Page

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  ....
</asp:ContentPlaceHolder>

<asp:Panel ID="PanelRightMenu" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
  ....
</asp:ContentPlaceHolder>
</asp:Panel>

Child Page code binding

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