7

Is it possible to call ASP.NET codebehind function from JavaScript.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Satyaprakash J
  • 81
  • 1
  • 2
  • 5
  • 1
    Why isn't this closed as a duplicate? http://stackoverflow.com/questions/3709426/call-server-side-function-from-client-side-javascript-closed – Ian Mercer Sep 15 '10 at 06:40
  • 1
    possible duplicate of [Calling ASP.NET server side method via JQuery](http://stackoverflow.com/questions/886903/calling-asp-net-server-side-method-via-jquery) – roryf Sep 15 '10 at 08:29

6 Answers6

11

I would prefer Muhammad Akhtar PageMethod approach. Just one short note: You don't need the scriptmanager. The scriptmanager only generates the javascript proxy methods for you. If you already have JQuery on your page, you can forget about the scriptmanager and write something like this on your page instead:

<script type ="text/javascript">
    $(document).ready(function() {
        $("#AjaxLink").click(function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "YourPage.aspx/updateContent",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    $("#content").html(result.d);
                }
            });
        });
     });
</script>

this assumes that you have a link with the ID AjaxLink on your page as well as a div with the id content that shows the result. The benefit is that you save 30kb javascript compared between jquery and the injected scripts by the scriptmanager

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Andre Kraemer
  • 2,633
  • 1
  • 17
  • 28
10

yes, you can make web method like..

<WebMethod(EnableSession:=True), ScriptMethod()> _ 
Public Shared Function updateContent() As String
      Return "Your String"
    End Function

and then call in javascript like..

PageMethods.updateTabContent(parameterValueIfAny, onSuccessMethod,onFailMethod);

this also need to add

<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
                </asp:ScriptManager>
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
4

cs function

[System.Web.Services.WebMethod]
public static string set_single_amount(arg a1)
{
     return value;
}

in script cs fucnction calling using scriptmanager

$("#control").click(function()
{
  PageMethods.set_single_amount(value,afercalling)
});

function afercalling(result)
{
alert(result);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
jithin
  • 41
  • 1
3

No, it is not possible to call ASP.NET code behind function from Javascript directly. ASP.NET code behind executes on the server in the context of the ASP.NET worker process. Javascript is executed on the client in the context of the client's browser.

The only way Javascript could trigger execution of ASP.NET code behind is through making an AJAX call from the Javascript to the server.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • @Aristos - how do you intend to call a server side method from the browser without using AJAX? – Michael Shimmins Sep 15 '10 at 06:39
  • @Michael You just call a page with your parameters, and on page you return 204 status code. Make a test to see it. :) – Aristos Sep 15 '10 at 08:01
  • @Aristos - you can't "call" a page. You can make an HTTP request, but nothing guarantees that there's another HTTP agent listening at the endpoint address, or that that agent will respond, or that the response will not be a caches copy from previous request returned by an intermediary. In other words - there's no guarantee a code will be executed as a result of that HTTP request. (well, broadly speaking, _a code_ will always be executed, of course, but it might well not be the code you want/expect to :-)) – Franci Penov Sep 15 '10 at 08:10
  • @Fanci @Michale I place an answer here with the 204 status code example. – Aristos Sep 15 '10 at 08:17
  • @Michael Yes you can not have feedback on your user that the code is finally run. Ok, this is just an example and a way to call it, not the perfect one, just a way. How ever this way if the code is not execute the 204 is not return back and an empty page appear that the page can not load. – Aristos Sep 15 '10 at 08:25
1

You can make use of __doPostBack to make a postback from javascript. Just add a server control with AutoPostBack property and set it to true.

See Calling __doPostBack in javascript

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
rahul
  • 184,426
  • 49
  • 232
  • 263
0

If you are working on a non-Ajax project, you can try System.Web.UI.ICallbackEventHandler.

Samples here:

http://msdn.microsoft.com/en-us/library/ms178210.aspx

http://www.ajaxmatters.com/2006/05/using-icallbackeventhandler-in-asp-net/

Community
  • 1
  • 1
prashanth
  • 2,059
  • 12
  • 13