0

I have a global function that returns some string. I need to access to that function from JavaScript in one of the pages and set returned value to JavaScript variable.

Example:-

var jsvariable = <%GlobalClass.MethodReturningString();%>;

How to do that?

5
  • What do you mean by global class? Is that method static? What happens when you try the code sample in your question? Commented Mar 10, 2014 at 9:12
  • Yes, the global class and method are static. When i use my sample i get site error: “The Controls collection cannot be modified because the control contains code blocks (ie. <% ... %>). Commented Mar 10, 2014 at 9:16
  • For the error you've stated, could you try the scriptlets with <%= %> or <%$ %> Commented Mar 10, 2014 at 9:23
  • Or <%# ... %> followed by DataBind(). Commented Mar 10, 2014 at 9:24
  • Read this related to your error. Commented Mar 10, 2014 at 9:47

4 Answers 4

0

You cannot call C# function like this. you need to create a web service or webmethod in order to call this function. Please see this link it will help you. http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx

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

Comments

0

Perform the following steps :

  1. Right click on your website or web application and add a WebHandler. The extension for the webhandler is '.ashx'. Let's say you name your handler as 'Handler.ashx'
  2. Open the Handler.ashx file and within the ProcessRequest method write the following : context.Response.Headers.Clear(); context.Response.Write(GlobalClass.MethodReturningString();); context.Response.Flush();
  3. In your javascript, perform an ajax call using jQuery :

    $.ajax({ url:'someUrl/Handler.ashx', type:'GET', success : function(data){ var someJavascriptVariable = data; } });

NOTE: This is a quick and dirty write so I'm not guaranteeing that the code will surely work, but it's a point for you to start with.

Comments

0

May i thick You cannot do this but u can access ur c# function using ajax

For Example

   $.post('url', {parameter : parameter }, function (result) {

         // here the result is your function return value or output

   },"json");

URL FORMAT : '/pageurl/methodname'

Comments

0

First of all, as a clarification, it seems that you're not trying to actually call a C# method from Javascript, but rather render the return from a C# method inside the page so that it can be used as a Javascript variable value on the client side.

In order to do that you need to update you syntax like:

var jsvariable = '<%= GlobalClass.MethodReturningString() %>';

Please note that if your class is not in the same namespace as the inherited page from the code behind file, then you need to import its namespace like below:

<%@ Import Namespace="GlobalClassNamaspace" %>

The namespace import can also be done globally (and it will be automatically available in all of the site's pages), using the web.config file as described here.

If you were to actually need to call a C# method from Javascript, which would have been needed if you wanted to dynamically use its results from client side code, then that could be accomplished through Ajax.

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.