如何在 ASP.NET 中创建两个具有相同 ID 的 ContentPlaceHolder?
我不想有两个母版页,所以我尝试这样做(为了可读性而省略了 <% %>):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有没有尝试过这样的:
Have you tried like this: