views:

43

answers:

3

Hello,

I have a long javascript in a string and programatically using RegisterClientScriptBlock, I add it to my page.

Is there any way to have the intellisense detect my javascript inside the string?

Code:

string Script0 =
@"
function dummy()
{
}

var PTRValues = new Array();
...
...
..
";

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myCustomScriptBlock", Script0, true);
+2  A: 

No, you can't get intellisense inside the JS string. The IDE doesn't know this particular string is JS.

If it's long don't put it in the *.cs file. Instead store it in a *.js. If you really want you can load the file into memory at runtime and serve it embedded in the html instead of referenced.

Sam
A: 

Unfortunately, this is not possible.

The best solution is to make put the code separate .js file, then write the following:

Page.ClientScript.RegisterClientScriptBlock(
    GetType(), 
    "myCustomScriptBlock", 
    File.ReadAllText(myJSFilePath), 
    true
);

For optimal performance, you should read it only once, then store in in the cache.

SLaks
A: 

Ok, these guys are getting close... Don't EVER embed scripts in code. Always embed as resource or for prototyping and develepment use ClientScript to render a <script/> tag and reference a .js file.

There are just too many reasons wny you would not want to embed script in code to list. google it.

What you are after is to render some javascript from the codebehind via ClientScript and you would like design time intellisense support?

Ok, To get intellisense you will need a .js of some kind. The approach I suggest, to promote maintainability and prevent dupe scripts that can get out of sync is:

  1. create an EMPTY file called myScript.js.

  2. create another script containing your code named myScript-vsdoc.js

  3. mark myScript-vsdoc.js as embedded resource and serve it as and embedded web resource

  4. meanwhile, back in the IDE, add a script tag pointing to myScript.js, which is an EMPTY file

  5. press SHIFT-CTRL-J and bingo, you have intellisense for your embedded script, your embedded script is in a source file that is editable and discoverable and you have no duplication.

That is how i do it.

Sky Sanders