从JavaScript调用ASP.NET代码隐藏功能

时间:2022-08-25 22:05:15

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

是否可以从JavaScript调用ASP.NET代码隐藏函数。

6 个解决方案

#1


9  

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..

然后在javascript中调用..

PageMethods.updateTabContent(parameterValueIfAny, onSuccessMethod,onFailMethod);

this also need to add

这也需要补充

<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
                </asp:ScriptManager>

#2


9  

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:

我更喜欢Muhammad Akhtar PageMethod方法。只需简短说明:您不需要脚本管理器。 scriptmanager只为您生成javascript代理方法。如果您的页面上已经有JQuery,那么您可以忘记脚本管理器并在页面上写下这样的内容:

<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

这假设您在页面上有ID AjaxLink的链接以及带有显示结果的id内容的div。好处是你可以通过scriptmanager在jquery和注入的脚本之间保存30kb的javascript

#3


3  

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);
}

#4


1  

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.

不,不可能直接从Javascript调用ASP.NET代码函数。 ASP.NET代码隐藏在ASP.NET工作进程的上下文中在服务器上执行。 Javascript在客户端浏览器的上下文中在客户端上执行。

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

Javascript可以触发执行ASP.NET代码的唯一方法是通过从Javascript到服务器进行AJAX调用。

#5


0  

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.

您可以使用__doPostBack从javascript进行回发。只需添加具有AutoPostBack属性的服务器控件并将其设置为true。

See Calling __doPostBack in javascript

请参阅在javascript中调用__doPostBack

#6


0  

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

如果您正在处理非Ajax项目,则可以尝试使用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/

#1


9  

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..

然后在javascript中调用..

PageMethods.updateTabContent(parameterValueIfAny, onSuccessMethod,onFailMethod);

this also need to add

这也需要补充

<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
                </asp:ScriptManager>

#2


9  

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:

我更喜欢Muhammad Akhtar PageMethod方法。只需简短说明:您不需要脚本管理器。 scriptmanager只为您生成javascript代理方法。如果您的页面上已经有JQuery,那么您可以忘记脚本管理器并在页面上写下这样的内容:

<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

这假设您在页面上有ID AjaxLink的链接以及带有显示结果的id内容的div。好处是你可以通过scriptmanager在jquery和注入的脚本之间保存30kb的javascript

#3


3  

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);
}

#4


1  

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.

不,不可能直接从Javascript调用ASP.NET代码函数。 ASP.NET代码隐藏在ASP.NET工作进程的上下文中在服务器上执行。 Javascript在客户端浏览器的上下文中在客户端上执行。

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

Javascript可以触发执行ASP.NET代码的唯一方法是通过从Javascript到服务器进行AJAX调用。

#5


0  

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.

您可以使用__doPostBack从javascript进行回发。只需添加具有AutoPostBack属性的服务器控件并将其设置为true。

See Calling __doPostBack in javascript

请参阅在javascript中调用__doPostBack

#6


0  

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

如果您正在处理非Ajax项目,则可以尝试使用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/