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

ske*_*ker 4 asp.net asp.net-mvc master-pages

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

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

但它不会让我:

Duplicate ContentPlaceHolder 'X' were found. ContentPlaceHolders require unique IDs.
Run Code Online (Sandbox Code Playgroud)

所以我尝试用 ID="<%= "X" %>" 设置 ID,不,也不让我这样做:

Server tags cannot contain <% ... %> constructs.
Run Code Online (Sandbox Code Playgroud)

然后我尝试了 <%# 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" />
Run Code Online (Sandbox Code Playgroud)

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

echo '<asp:ContentPlaceHolder ID="X" runat="server" />'
Run Code Online (Sandbox Code Playgroud)

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

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

Dar*_*rov 5

你有没有尝试过这样的:

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

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

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

if (isAEqualB)
{
    </div>
}
Run Code Online (Sandbox Code Playgroud)