Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how can call any method in c# from javascript ???
Posted
Comments
ZurdoDev 23-Sep-14 10:45am    
There are various ways and it would be helpful to understand what you are trying to do and where you are stuck.

I presume you're looking to call a server side function from javascript, yes?
Then google will help you a lot: https://www.google.de/?gws_rd=ssl#q=calling+server+side+function+from+javascript[^]

This looks like a promising link: http://www.c-sharpcorner.com/uploadfile/rohatash/calling-server-side-function-from-javascript-in-asp-net/[^]

You might also want to look into the code attribute [WebMethod].

Cheers!
 
Share this answer
 
You never ever really call any server-side method from Javascript, which is always called on the client side, by a browser. (I don't count rare cases of server-side Javascript — you hardly use it if you have tagged "ASP.NET").

All you can do is to send some HTTP request to the server and get HTTP response. The resource pointed by the URL used in such request should be programmed to interpret such request and provide appropriate responses. From the Javascript, it is done using Ajax: http://en.wikipedia.org/wiki/Ajax_(programming)[^].

A very convenient way of doing so is jQuery Ajax: http://api.jquery.com/jquery.ajax[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

—SA
 
Share this answer
 
v2
Make C# method Static and Tag it with [WebMethod] attribute
like this :-

[WebMethod]
public static void TestMethod()
{
}

javascript code
=================
XML
<script>
    function test(){
        alert(PageMethods.TestMethod());
    }
</script>
 
Share this answer
 
You can call as many functions inside your javascript.
You need to define the function, may be outside the document.ready and then can call anywhere you like . Below is a small snippet:
C#
$(document).ready(function(){
$("#ANYID").click(function(){
MyFunction(parameters);//with parameter
MyFunction2();//without parameter
})

});
function MyFunction(parameters){
//do something
}

function MyFunction2(parameters){
//do something
}
 
Share this answer
 
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Sep-14 10:52am    
First, OP asks about the opposite: calling C# method from Javascript (which never happens).
But calling Javascript function from server side also never happens, despite the advice you reference, with registering a script; it does not really calls the Javascript function. Script is generated and called by the browser.
—SA

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