为什么我不能在 ASP.NET 中的 HTML 属性内使用 ContentPlaceholder?
我正在使用 ASP.NET MVC2 和 C#,但这个问题通常适用于 ASP.NET。
这会破坏:
<body id="<asp:ContentPlaceHolder ID="BodyID' runat="server" />">
Intellisense 在 body
标记下划线,并在 id=
之后立即开始引号,并抱怨:
验证 (HTML 4.01):元素“body”缺少“>”从其开始标记开始的字符。
asp 元素被忽略,并且 id 属性在呈现的 HTML 中为空。 (无论我在 ASP 元素内使用双引号还是单引号,都会出现同样的问题,但后者会破坏 VS 中的语法高亮显示。)
这是有效的(假设我设置了会话变量):
<body id="<%: Session["BodyID"] %>">
为什么 HTML 属性内部支持内联计算,但 ASP控件不会渲染内部属性?
这是我的用例:根据从控制器传递的数据,视图知道它正在渲染什么类型的数据。该视图将数据注入母版页中的各个位置。我可以将标题注入头部,将标记注入正文——但我也想将数据注入一些属性。 ID 和类名是明显的例子,但还有其他例子。
我想在保持有效标记的同时执行此操作;没有动态呈现整个正文标记之类的技巧——我希望页面在 Visual Studio 中始终看起来像有效的 HTML 或 XML 文档。
使用内联 eval 是可以的,但它需要我设置属性,这是我在模型或控制器中所做的。在某些情况下这是必要的,但在其他情况下,值是静态的——我有一个专门构建的视图,我只需要将视图中的静态值注入到母版页中。我不想经历创建抽象控制器类、让所有控制器继承它等的所有开销,只是为了获得在注入标记时已经拥有的相同功能。
附带问题(是的,我应该为此提出一个单独的问题):ASP 控件和内联代码块的评估顺序是什么?我假设代码块在 ASP 控件之前首先被解析,因此我可以将代码块放入 ASP 控件声明中。但我找不到详细说明该过程的文档——有人可以向我指出吗?
谢谢!
更新: Pauli 提到,您确实可以在任何地方使用 ContentPlaceholder,只要它们不在已标记为 runat="server"
的元素内即可。我再次测试,发现他是正确的——我最初错过了这一点。 Visual Studio 仍然感到困惑并给出 HTML 验证警告,但在呈现页面时预期值出现在属性中。所以,问题的答案是“但是你可以!”
I'm using ASP.NET MVC2 and C#, but this question applies to ASP.NET in general.
This breaks:
<body id="<asp:ContentPlaceHolder ID="BodyID' runat="server" />">
Intellisense underlines the body
tag and the opening quote immediately after id=
, and complains:
Validation (HTML 4.01): Element 'body' is missing the '>' character from its start tag.
The asp element is ignored, and the id attribute is empty in the rendered HTML. (Same problem whether I use double or single quotes inside the ASP element, tho the latter breaks syntax hilighting in VS.)
This works (assuming I set the session variable):
<body id="<%: Session["BodyID"] %>">
Why is it that inline evaluation is supported inside HTML attributes, but ASP controls won't render inside attributes?
Here's my use case: based on data passed from the controller, the view knows what type of data it's rendering. The view injects data into various places in the master page. I can inject a title into the head, and markup into the body -- but I also want to inject data into some attributres. IDs and class names are the obvious examples, but there are others.
I want to do this while still maintaining valid markup; no tricks like dynamically rendering the entire body tag -- I want a page that looks like a valid HTML or XML doc at all times in Visual Studio.
Using inline eval is OK, but it requires me to set properties, which I do in the model or controller. In some cases that's necessary, but in others the values are static -- I have a view that's purpose-built, and I just need to inject a static value from the view into the master page. I don't want to go thru all the overhead of creating an abstract controller class, having all my controllers inherit from it, etc. just to get to the same functionality I already have when I'm injecting markup.
Side question (yes, I should open a separate question for it): What's the order of evaluation of ASP controls and inline code nuggets? I assume the code nuggets are resolved first, before the ASP controls, so I could e.g. put a code nugget inside an ASP control declaration. But I can't find docs that detail the process -- can anyone point them out to me?
Thanks!
UPDATE: Pauli mentioned that you can indeed use ContentPlaceholders anywhere you like, as long as they're not inside an element that's already marked runat="server"
. I tested again, and see he's correct -- I missed that initially. Visual Studio is still confused and gives an HTML validation warning, but the expected value appears in the attribute when the page is rendered. So, the answer to the question is "But you can!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为body标签没有runat =“server”,所以它不被视为服务器控件,但只是清晰的文本,因此您可以在任何您想要的地方放置内容占位符或任何其他控件。
Since the body tag doesn't have runat="server" its not treated as a server control, but just clear text and you can therefor put a contentplaceholder, or whatever other control wherever you want.