Calling ASP.NET Code Behind function from JavaScript

前端 未结 6 728
梦毁少年i
梦毁少年i 2020-11-29 08:45

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

相关标签:
6条回答
  • 2020-11-29 09:18

    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

    0 讨论(0)
  • 2020-11-29 09:19

    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>
    
    0 讨论(0)
  • 2020-11-29 09:27

    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

    0 讨论(0)
  • 2020-11-29 09:33

    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/

    0 讨论(0)
  • 2020-11-29 09:45

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 09:45

    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.

    0 讨论(0)
提交回复
热议问题