Call ASP.NET function from JavaScript?

后端 未结 20 2183
暗喜
暗喜 2020-11-22 07:21

I\'m writing a web page in ASP.NET. I have some JavaScript code, and I have a submit button with a click event.

Is it possible to call a method I created in ASP with

20条回答
  •  孤独总比滥情好
    2020-11-22 08:12

    The Microsoft AJAX library will accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions.

    I suggest the Microsoft AJAX library. Once installed and referenced, you just add a line in your page load or init:

    Ajax.Utility.RegisterTypeForAjax(GetType(YOURPAGECLASSNAME))
    

    Then you can do things like:

     _
    Public Function Get5() AS Integer
        Return 5
    End Function
    

    Then, you can call it on your page as:

    PageClassName.Get5(javascriptCallbackFunction);
    

    The last parameter of your function call must be the javascript callback function that will be executed when the AJAX request is returned.

提交回复
热议问题