如何在 ASP.NET 中创建两个具有相同 ID 的 ContentPlaceHolder?

发布于 2024-11-15 12:22:23 字数 958 浏览 1 评论 0原文

我不想有两个母版页,所以我尝试这样做(为了可读性而省略了 <% %>):

if (a == b)
{
    <asp:ContentPlaceHolder ID="X" runat="server" />
}
else
{
    <div class="c">
        <asp:ContentPlaceHolder ID="X" runat="server" />
    </div>
}

但它不会让我这样做:

Duplicate ContentPlaceHolder 'X' were found. ContentPlaceHolders require unique IDs.

所以我尝试使用 ID= 设置 ID “<%=“X”%>”不,也不让我:

Server tags cannot contain <% ... %> constructs.

然后我尝试了 <%# Eval("X") %>不:

The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

有什么办法可以实现我想要做的事情吗?我正在考虑类似

echo '<asp:ContentPlaceHolder ID="X" runat="server" />'

或者某种动态方式来添加标签,因为显然解析器无法识别 if else 块,它不会让两个标签具有相同的 ID。

我使用 MVC 和默认视图引擎。

I don't want to have two master pages so I'm trying to do this (left out the <% %> for readability):

if (a == b)
{
    <asp:ContentPlaceHolder ID="X" runat="server" />
}
else
{
    <div class="c">
        <asp:ContentPlaceHolder ID="X" runat="server" />
    </div>
}

But it won't let me:

Duplicate ContentPlaceHolder 'X' were found. ContentPlaceHolders require unique IDs.

So I tried to set the IDs with ID="<%= "X" %>" and no, won't let me either:

Server tags cannot contain <% ... %> constructs.

Then I tried <%# Eval("X") %> and nope:

The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

Is there any way to achieve what I'm trying to do? I was thinking something like

echo '<asp:ContentPlaceHolder ID="X" runat="server" />'

Or some dynamic way to add the tag because apparently the parser can't identify the if else block which won't let two tags have the same ID.

I'm using MVC with the default view engine.

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

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

发布评论

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

评论(1

草莓酥 2024-11-22 12:22:24

你有没有尝试过这样的:

<% var isAEqualB = a == b; %>

if (isAEqualB)
{
    <div class="c">
}

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

if (isAEqualB)
{
    </div>
}

Have you tried like this:

<% var isAEqualB = a == b; %>

if (isAEqualB)
{
    <div class="c">
}

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

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