为什么我不能在 ASP.NET 中的 HTML 属性中使用 ContentPlaceholder? [英] Why can't I use a ContentPlaceholder inside HTML attributes in ASP.NET?

查看:28
本文介绍了为什么我不能在 ASP.NET 中的 HTML 属性中使用 ContentPlaceholder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ASP.NET MVC2 和 C#,但这个问题一般适用于 ASP.NET.

I'm using ASP.NET MVC2 and C#, but this question applies to ASP.NET in general.

这中断了:

<body id="<asp:ContentPlaceHolder ID="BodyID' runat="server" />">

Intellisense 在 body 标签和紧跟在 id= 之后的开场引号下划线,并抱怨:

Intellisense underlines the body tag and the opening quote immediately after id=, and complains:

验证 (HTML 4.01):元素body"在其开始标记中缺少>"字符.

Validation (HTML 4.01): Element 'body' is missing the '>' character from its start tag.

asp 元素被忽略,并且 id 属性在渲染的 HTML 中为空.(无论我在 ASP 元素中使用双引号还是单引号,都存在同样的问题,但后者会破坏 VS 中的语法高亮显示.)

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"] %>">

为什么 HTML 属性内部支持内联求值,而 ASP 控件不会在属性内部呈现?

Why is it that inline evaluation is supported inside HTML attributes, but ASP controls won't render inside attributes?

这是我的用例:基于从控制器传递的数据,视图知道它正在呈现什么类型的数据.该视图将数据注入母版页中的不同位置.我可以将标题注入头部,并将标记注入主体——但我也想将数据注入某些属性.ID 和类名是明显的例子,但还有其他的.

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.

我想在保持有效标记的同时做到这一点;没有像动态渲染整个 body 标签这样的技巧——我想要一个在 Visual Studio 中始终看起来像有效 HTML 或 XML 文档的页面.

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.

使用内联 eval 是可以的,但它需要我设置属性,我在模型或控制器中进行.在某些情况下这是必要的,但在其他情况下,这些值是静态的——我有一个专门构建的视图,我只需要从视图中将一个静态值注入母版页.我不想经历创建抽象控制器类的所有开销,让我的所有控制器都继承自它等等,只是为了获得我在注入标记时已经拥有的相同功能.

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.

附带问题(是的,我应该为它打开一个单独的问题):ASP 控件和内联代码块的评估顺序是什么?我假设在 ASP 控件之前首先解决代码块,所以我可以例如将代码块放在 ASP 控件声明中.但是我找不到详细说明该过程的文档 - 谁能向我指出它们?

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?

谢谢!

更新: Pauli 提到您确实可以在任何您喜欢的地方使用 ContentPlaceholder,只要它们不在已标记为 runat="server" 的元素内.我再次测试,发现他是正确的——我最初错过了.Visual Studio 仍然困惑并给出 HTML 验证警告,但在呈现页面时属性中出现预期值.所以,这个问题的答案是但你可以!"

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!"

推荐答案

  1. 你不应该在 MVC 中使用 WebForms 控件
  2. 在您的 ID 和 runat 周围使用 '',因为解析器会混淆您所有冲突的 "'s

由于 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.

这篇关于为什么我不能在 ASP.NET 中的 HTML 属性中使用 ContentPlaceholder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆