Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a javascript funtion JA which accepts a string parameter, and a c# function CB which accepts string parameter

e.g

//js
function JA(location)
{

}


//c#
public string calculate(string par)
{

//proccess and return string
}

now i want to call my javascript function i.e JA such that i pass the return value of c# as parameter to this javascript function

i.e
<script>
JA(calculate())
</Script>
Posted
Comments
Kannannns 7-Aug-14 6:46am    
$.ajax({
type: "POST",
url: "../loadmapstring.asmx/bindMapLocations",
data: "{s:sParam}", // passing the parameter
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(retValue) {
// Do something with the return value from.Net method
generateMarkersForSideBar(retValue);
}
});


i tried this but its not working

C#
public static void calculate(string par)
      {
          // Cleans the message to allow single quotation marks
          string cleanMessage = par.Replace("'", "\\'");
          string script = string.Format("<script type=\"text/javascript\">$(document).ready(function(){JA('{0}');});</script>", cleanMessage);

          // Gets the executing web page
          Page page = HttpContext.Current.CurrentHandler as Page;
          // Checks if the handler is a Page and that the script isn't allready on the Page
          if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("JA"))
          {
              page.ClientScript.RegisterClientScriptBlock(typeof(<your class="" name="">), "JA", script);
          }
      }</your>
 
Share this answer
 
Comments
Kannannns 7-Aug-14 5:55am    
but how do i call this C# function?
You can using ajax jquery

JavaScript
function JA(location)
{
$.ajax({
            type: 'POST',
            url: "ControllerName/calculate",
            data: dataForPost,
            success: function (returnValue) {}
        });
 
}


http://www.w3schools.com/jquery/ajax_ajax.asp[^]
 
Share this answer
 
make an ajax call
and if using web forms, then make the c# method as [WebMethod] before ajax call.
 
Share this answer
 
Comments
Kannannns 8-Aug-14 0:28am    
$.ajax({
type: "POST",
url: "../loadmapstring.asmx/bindMapLocations",
data: "{s:sParam}", // passing the parameter
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(retValue) {
// Do something with the return value from.Net method
generateMarkersForSideBar(retValue);
}
});


i tried this but its not working
Hi,
Use this line after getting result from your C# function


Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "sScript", "<script language='javascript'>MyJSfunction( PassResultStringOfC#CodeAsParameter );</script>");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900