母版页和页面中的嵌入代码块

发布于 2024-08-05 10:51:54 字数 1047 浏览 3 评论 0原文

嗯,我有点困惑!

“简单的问题”

我的母版页的某些部分我想根据 ContentPlaceHolder 是否为空来以不同的方式呈现。

所以:

我有一个母版页,就像一些代码一样:

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="LittleDiv">
<% } else { %>
    <div id="BigDiv">
<% } %>
        some text
    </div>

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="Div1">
         yadda yadda yadda
         [<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"/>]
         blah blah blah
    </div>
<% } %>

我有一个页面,其中有一些代码:(

<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">
   <% if (Model.SomeValue) { %>
       hello! 
   <% } %>
</asp:Content>

注意:示例页面的嵌入代码块中的逻辑对于每个内容占位符、每个页!)

这看起来不错,但不起作用。 问题正如我所见(警告我可能完全错了!)。嵌入式代码块在对象渲染中进行评估。有点反直觉的是页面似乎控制了master。因此母版页的 Render 在页面渲染之前被调用。这意味着母版页始终可以看到页面内容控件中的内容。

有没有好的办法解决这个问题? (我有一些解决方案,但这是一个可怕的黑客!我不会发布它,因为我不想影响思考。)

Well I'm some what baffled!

'Simple problem'

I have parts of my master page that I want to render differently depending on whether or not a ContentPlaceHolder is empty.

So:

I have a master page that as some code like:

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="LittleDiv">
<% } else { %>
    <div id="BigDiv">
<% } %>
        some text
    </div>

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="Div1">
         yadda yadda yadda
         [<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"/>]
         blah blah blah
    </div>
<% } %>

I have a page that has some code along the lines of:

<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">
   <% if (Model.SomeValue) { %>
       hello! 
   <% } %>
</asp:Content>

(note: the logic in the example page's embeded code block may be unique, for each content place holder, for each page!)

Which seems nice however it doesn't work.
The problem is as I see it (warning I might be completely wrong!). The Embedded Code Blocks are evaluated in the objects Render. A little counter intuitively the page seems to control the master. So the master page's Render is called before the pages render. This means that the master page always sees content in the page's content control.

Is there a nice way round this?
(I have a bit of a solution but it is a monstrous hack! I won't post it yet coz I don't want to influence thinking.)

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

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

发布评论

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

评论(3

月寒剑心 2024-08-12 10:51:54

好的,这是另一个想法:)。与其将逻辑放在母版页中...

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="LittleDiv">
<% } else { %>
    <div id="BigDiv">
<% } %>
        some text
    </div>

为什么不为其创建一个占位符?例如,

<asp:ContentPlaceHolder ID="LittleBigDivPlaceHolder" runat="server" />

您可以让具体的非母版页来决定内容。您还可以通过将逻辑移动到部分视图中来避免重复,该部分视图可以由您的不同视图参数化。

OK, here's another idea :). Instead of putting the logic in your master page...

<% if (ContentPlaceHolder1.HasControls()) {%>
    <div id="LittleDiv">
<% } else { %>
    <div id="BigDiv">
<% } %>
        some text
    </div>

Why not create a placeholder for it? e.g.

<asp:ContentPlaceHolder ID="LittleBigDivPlaceHolder" runat="server" />

You could then allow your concrete non-master pages to decide the content. You could also avoid repeating yourself by moving the logic into a Partial View that could be, say, parameterised by your different views.

冷夜 2024-08-12 10:51:54

您的母版页可以检查 ViewData 字典的内容并从那里工作其逻辑,而不是寻找结果控件吗?

Rather than looking for the resultant controls can your master page examine the contents of the ViewData dictionary and work its logic from there?

凝望流年 2024-08-12 10:51:54

或者,您可以使用 javascript 在客户端上显示/隐藏 div。

Alternatively, you could use javascript to show / hide divs on the client.

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