ASP.NET 内联服务器标记
首先我想说我的代码运行完美,这更像是一个“如何最好地做到这一点”的问题。
所以我的 .aspx
文件中有这样的代码:
function EditRelationship() {
var projects=<%= GetProjectsForEditRelationship() %>;
// fill in the projects list
$('#erProjectsSelect').empty();
for(var i in projects)
$('#erProjectsSelect').append('<option value='+projects[i][0]+'>'+projects[i][1]+'</option>');
var rels=<%= GetRelationshipsForEditRelationship() %>;
// etc
}
再次,它工作正常。问题是 VS2008 有点卡住这样的代码,它在标签中的 <
字符下划线(带有相关的警告),然后拒绝为 javascript 的其余部分提供代码完成。 它也拒绝再格式化我的文档,出现解析错误。最后一部分是我最烦恼的部分。
我想我可以将其中一些放在 eval 中,但是添加额外的层和运行时性能命中只是为了关闭 VS 似乎有点愚蠢,而且它并不总是一个选项(我不记得了)在我的脑海中,这不是一个选择,但相信我,我有一个奇怪的构造)。
所以我的问题是,你如何最好地写这个(最好意味着VS投诉最少)?在我看来,eval 和 ajax 调用都不适合这个。
I'd like to start by saying that my code is working perfectly, this is more a "how best to do it" kind of question.
So I have code like this in my .aspx
file:
function EditRelationship() {
var projects=<%= GetProjectsForEditRelationship() %>;
// fill in the projects list
$('#erProjectsSelect').empty();
for(var i in projects)
$('#erProjectsSelect').append('<option value='+projects[i][0]+'>'+projects[i][1]+'</option>');
var rels=<%= GetRelationshipsForEditRelationship() %>;
// etc
}
Again, it's working fine. The problem is that VS2008 kinda chokes on code like this, it's underlining the <
character in the tags (with associated warnings), then refusing to provide code completion for the rest of the javascript. It's also refusing to format my document anymore, giving parsing errors. The last part is my worst annoyance.
I could put some of these in eval
s I guess, but it seems sorta dumb to add additional layers and runtime performance hits just to shut VS up, and it's not always an option (I can't remember off the top of my head where this wasn't an option but trust me I had a weird construct).
So my question is, how do you best write this (where best means fewest VS complaints)? Neither eval
nor ajax calls fit this imo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的目标是减少 VS 投诉,并且您正在运行 asp.net 4(支持静态客户端 ID),也许像下面这样的策略会更好?
这比您的解决方案需要更多工作,并且您将向回发添加一些数据(如果您执行任何操作),但我想它不会引起任何 VS 投诉:)
If your aim is to reduce VS complaints, and if you are running asp.net 4 (supporting Static client Ids), maybe a strategy like the following would be better?
It's more work than your solution, and you will add some data to the postback (if you perform any) but it won't cause any VS complaints I guess :)
您可以在代码隐藏的页面中执行此操作
,或者构建类似 JSON 的内容,您可以将其写出来
更新根据我的评论
You could do this from your page in the code-behind
or to construct something like JSON you could write it out
UPDATE Based on my comment
我没有安装 VS2008 来测试,所以对此持保留态度,但是你尝试过这样的事情吗?
类似的事情可能会欺骗 JavaScript 解析器忽略表达式的内容。
无论如何,VS2010 正确地解析并突出显示了您的原始代码片段。
I don't have VS2008 installed to test with, so take this with a grain of salt, but have you tried something like this?
Something like that might trick the JavaScript parser into ignoring the content of your expression.
For what it's worth, VS2010 correctly parses and highlights your original code snippet.
是否可以将其移至 VS2010?我刚刚复制并粘贴了您的代码,IDE 正确解释了它。
Is it an option to move this to VS2010? I just copied and pasted your code and the IDE interpreted it correctly.
最好的解决方案是将 javascript 放在单独的文件中并完全避免这种情况。对于这个特定的功能,您正在执行服务器端工作。为什么不构建您打算在代码隐藏中动态添加的选项列表,将它们放入隐藏的 div 中,然后让 jQuery 从已渲染的 HTML 中添加它们?
如果您确实想以这种方式动态创建大量 javascript,请考虑在代码隐藏中使用 ScriptManager 将您需要的变量设置为脚本并注册它们,那么您的内联脚本将不需要转义
(基本上,这不是完整的语法,它取决于上下文)。然后在您的函数中引用“项目”。
(编辑)
在更大范围内执行此操作的一种更干净的方法,在代码隐藏中设置您需要的所有内容:
并引用以下内容:
如果您经常这样做,当然,您可以创建一个类来自动化构建脚本的。
The best solution is to put javascript in a separate file and avoid this entirely. For this particular function, you're doing server-side work. Why not build the list of options that you intend to add dynamically in codebehind, put them in a hidden div, and then just have jQuery add them from the already-rendered HTML?
If you have a situation where you really want to dynamically create a lot javascript this way, consider using ScriptManager in codebehind to set up the variables you'll need as scripts and register them, then your inline script won't need to escape
(Basically, that is not the complete syntax, which is context dependent). Then refer to "projects" in your function.
(edit)
A little cleaner way to do this on a larger scale, set up everything you need like this in codebehind:
and refer to everything like:
If you do this a lot, of course, you can create a class to automate the construction of the script.