开发者

ASP.NET inline server tags

开发者 https://www.devze.com 2023-02-03 06:11 出处:网络
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.

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.


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


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>


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消