[RESOLVED] Inject Javascript From CodeBehind

Hello,

i am writing a web application in c#.I have defined some static javascript functions on a page.Those functions take as input some arguments that i want to pass from code behind.The problem is that i want to call these functions more than once, so i cannot define some variables and pass the parametres to the page.I want to inject the javascript that call the other javascript functions from code behind.Right now i am doing this using the RegisterStartupScript but this is not very convinient because i have to use escape characters etc.I have seen in some pages that javascript is writtenn on page using //<![CDATA[   JS code //]]> .Can someone help me inject code with this way?

Sorry for my English.


The CDATA solution wont help you in this scenario. This is for xhtml pages that want to be valid markup so you have to hide the javascript. Unfortunately the reason that you have to escape the javascript in the code behind is because you are putting it into a .net string.

You could use the @ modifier in some situations and it would reduce the amount of escaping you are doing:

 

One option is to put the javascript in an external file and use RegisterClientScriptInclude() to reference the external js file:

Put all of the static reusable code in there and then use the existing method of RegisterStartupScript simply to tie together your controls and the methods.

thoughts on "[RESOLVED] Inject Javascript From CodeBehind"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>