ASP.NET 内联服务器标记

发布于 2024-10-11 01:39:23 字数 880 浏览 4 评论 0原文

首先我想说我的代码运行完美,这更像是一个“如何最好地做到这一点”的问题。

所以我的 .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 evals 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿港巷口少年归 2024-10-18 01:39:23

如果您的目标是减少 VS 投诉,并且您正在运行 asp.net 4(支持静态客户端 ID),也许像下面这样的策略会更好?

  1. 创建一个 ASP:HiddenField 控件,将其 ClientIdMode 设置为“静态”
  2. 在页面加载时将 GetRelationshipsForEditRelationship() 的值分配给此字段
  3. 在您的 javascript 中,改为从隐藏字段读取值,我假设您知道如何执行此操作。

这比您的解决方案需要更多工作,并且您将向回发添加一些数据(如果您执行任何操作),但我想它不会引起任何 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?

  1. Create a ASP:HiddenField control, set its ClientIdMode to "Static"
  2. Assign the value of GetRelationshipsForEditRelationship() to this field on page load
  3. In your javascript, read the value from the hidden field instead, I assume you know how to do this.

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 :)

末蓝 2024-10-18 01:39:23

您可以在代码隐藏的页面中执行此操作

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

,或者构建类似 JSON 的内容,您可以将其写出来

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

更新根据我的评论

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

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

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>
∝单色的世界 2024-10-18 01:39:23

我没有安装 VS2008 来测试,所以对此持保留态度,但是你尝试过这样的事情吗?

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

类似的事情可能会欺骗 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?

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

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.

风筝有风,海豚有海 2024-10-18 01:39:23

是否可以将其移至 VS2010?我刚刚复制并粘贴了您的代码,IDE 正确解释了它。

Is it an option to move this to VS2010? I just copied and pasted your code and the IDE interpreted it correctly.

锦上情书 2024-10-18 01:39:23

最好的解决方案是将 javascript 放在单独的文件中并完全避免这种情况。对于这个特定的功能,您正在执行服务器端工作。为什么不构建您打算在代码隐藏中动态添加的选项列表,将它们放入隐藏的 div 中,然后让 jQuery 从已渲染的 HTML 中添加它们?

如果您确实想以这种方式动态创建大量 javascript,请考虑在代码隐藏中使用 ScriptManager 将您需要的变量设置为脚本并注册它们,那么您的内联脚本将不需要转义

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

(基本上,这不是完整的语法,它取决于上下文)。然后在您的函数中引用“项目”。

(编辑)

在更大范围内执行此操作的一种更干净的方法,在代码隐藏中设置您需要的所有内容:

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

并引用以下内容:

servervars.GetProductsForEditRelationship

如果您经常这样做,当然,您可以创建一个类来自动化构建脚本的。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文