简体   繁体   English

ASP.NET内联服务器标签

[英]ASP.NET inline server tags

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: 所以我的.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
    }

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. 问题是VS2008对这样的代码有点窒息,它强调了标签中的<字符(带有相关警告),然后拒绝为其余的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). 我可以把它们中的一些放进eval ,但是添加额外的图层和运行时性能命中只是为了关闭VS,这似乎有点愚蠢,而且它并不总是一个选项(我不记得我的头顶在哪里这不是一个选择,但相信我,我有一个奇怪的结构)。

So my question is, how do you best write this (where best means fewest VS complaints)? 所以我的问题是, 你如何最好地写这个 (哪里最好意味着VS投诉最少)? Neither eval nor ajax calls fit this imo. eval和ajax调用都不适合这个imo。

You could do this from your page in the code-behind 您可以在代码隐藏中从您的页面执行此操作

ClientScript.RegisterArrayDeclaration("projects", "1, 2, 3, 4");

or to construct something like JSON you could write it out 或者构建类似JSON的东西,你可以写出来

ClientScript.RegisterClientScriptBlock(GetType(), "JSONDeclarations", "your json stuff");

UPDATE Based on my comment 更新根据我的评论

<script id="declaration" type="text/javascript">
    var projects=<%= GetProjectsForEditRelationship() %>;
    var rels=<%= GetRelationshipsForEditRelationship() %>;
</script>
<script type="text/javascript">
    function EditRelationship() {
        // fill in the projects list
        $('#erProjectsSelect').empty();
        for(var i in projects)
            $('#erProjectsSelect').append('<option value='+projects[i][0]+'>'+projects[i][1]+'</option>');
}
</script>

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? 如果您的目标是减少VS投诉,并且如果您正在运行asp.net 4(支持静态客户端ID),那么像以下这样的策略会更好吗?

  1. Create a ASP:HiddenField control, set its ClientIdMode to "Static" 创建一个ASP:HiddenField控件,将其ClientIdMode设置为“Static”
  2. Assign the value of GetRelationshipsForEditRelationship() to this field on page load 在页面加载时将GetRelationshipsForEditRelationship()的值分配给此字段
  3. In your javascript, read the value from the hidden field instead, I assume you know how to do this. 在您的javascript中,请从隐藏字段中读取值,我假设您知道如何执行此操作。

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 :) 这比你的解决方案更多的工作,你将添加一些数据到回发(如果你执行任何)但它不会导致任何VS投诉我猜:)

The best solution is to put javascript in a separate file and avoid this entirely. 最好的解决方案是将javascript放在一个单独的文件中,并完全避免这种情况。 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? 为什么不构建你想要在代码隐藏中动态添加的选项列表,将它们放在一个隐藏的div中,然后让jQuery从已经呈现的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 如果您确实希望以这种方式动态创建很多javascript,请考虑在代码隐藏中使用ScriptManager来设置您需要的变量作为脚本并注册它们,然后您的内联脚本将不需要转义

ScriptManager.RegisterClientScript("projects = " + GetProductsForEditRelationship());

(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: 更大规模地执行此操作的一种更简洁的方法,在代码隐藏中设置您需要的所有内容:

string script = "var servervars = {" +
  "GetProductsForEditRelationship: " + GetProductsForEditRelationship() + 
  "GetRelationshipsForEditRelationship: " + GetRelationshipsForEditRelationship() +
"}"

and refer to everything like: 并指的是:

servervars.GetProductsForEditRelationship

If you do this a lot, of course, you can create a class to automate the construction of the script. 当然,如果你这么做了很多,你可以创建一个类来自动构建脚本。

I don't have VS2008 installed to test with, so take this with a grain of salt, but have you tried something like this? 我没有安装VS2008进行测试,所以带上一粒盐,但你有没有试过这样的东西?

var projects = (<%= GetProjectsForEditRelationship() %>);

Something like that might trick the JavaScript parser into ignoring the content of your expression. 这样的事情可能会欺骗JavaScript解析器忽略表达式的内容。

For what it's worth, VS2010 correctly parses and highlights your original code snippet. 对于它的价值,VS2010正确解析并突出显示您的原始代码段。

Is it an option to move this to VS2010? 是否可以将其移至VS2010? I just copied and pasted your code and the IDE interpreted it correctly. 我只是复制并粘贴了你的代码,IDE正确地解释了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM