0

In my class library i'm embedding some js and css files for use in my asp.net application, as in the method described here. Now i would to put some c# code inside js file, and let it compile in the resulting dll. Would it be possibile?

Example: somefile.js

function foo() 
{
    var a = "<%= SOME_CONSTANT_DECLARED_OUTSIDE %>";
    <% ... some conditional code %>
}

3 Answers 3

1

Javascript is client SIDE and C# is server side .

You can't run C# in Javascript

Sign up to request clarification or add additional context in comments.

1 Comment

thats not completely true. thats as you would say, you cannot use C# in HTML. But an ascx file IS a HTML file but running through a C# view template interpreter
1

ASP.NET does not compile, nor look for managed code (code that compiles, like C# code) in *.js files. SO when you put some C# code in js file, that code will not work as C# code (the client will try to use it as javascript code).

But there is a workaround - ASP.NET does render C# code in aspx files. So you can embed some C# code in JS function, as long as this function located in aspx file (inside script tags).

Comments

0

The documentation tells you a way how to add a variable..-

In the UpdatePanelAnimation.js you have this lines of code

BorderAnimation = function(color) {
    this._color = color;
}

this is then automatically set.

Dim script As String = String.Format( _
               CultureInfo.InvariantCulture, _
               "Sys.Application.add_load(function(sender, args) {{var {0}_borderAnimation = new BorderAnimation('{1}');var panelElement = document.getElementById('{0}');if (args.get_isPartialLoad()) {{{0}_borderAnimation.animate(panelElement);}}}});", _
               updatePanel.ClientID, _
               ColorTranslator.ToHtml(BorderColor))


        ScriptManager.RegisterStartupScript( _
            Me, _
            GetType(UpdatePanelAnimationWithClientResource), _
            ClientID, _
            script, _
            True)

So the formatted {1} is replaced with BorderColor that you have on the server side. You might now want to update the JS constructor like this

BorderAnimation = function(color, otherValue) { this._color = color; this._otherValue = otherValue }

now you do the same with the server side formatting but then you do

...
var {0}_borderAnimation = new BorderAnimation('{1}', '{2}')
...

this is the way how ASP.NET kind of 'connects' Backend with Frontend. There are some other ways (more elegant) to do that, but thats not the question :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.