3

With my javascript getting bigger and bigger, I want to clean up my ASPX File.

The problem is alot of my javascript involves a lot of inline C# calls. ie:

'<%=C# method call)%>'

But this doesn't seem to work when its tucked away in a .js file.

Is there a workaround so I can seperate my javascript into js files?

0

2 Answers 2

2

Certainly you cannot call C# from a .js file. C# code is executed on the server, not on the client. Your .js file is not parsed on the server.

Nothing should prevent you from putting actual JavaScript into a .js file, as opposed to, say, JavaScript that is dynamically emitted by server-side C# code.

If you have a lot of C# code that is generating JavaScript, you can still organize your code by using partial views (that's MVC terminology, but I'm sure there's something similar for standard ASP.Net).

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

Comments

0

You could use WebMethods and call them via Ajax.

[WebMethod]
public static int SomeMethod()
{
    ...
}

Then call it with Ajax. (I'm using jQuery in this example.)

$.ajax({
    type:        "POST"  
    url:         "/yourwebpage.aspx/SomeMethod"
    data:        "{}"
    contentType: "application/json; charset=utf-8",
    dataType:    "json",
    success:     function(data) {
                     // do something with the return value
                 }
});

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.