如何从javascript调用服务器端功能 [英] how to call server side function from javascript

查看:82
本文介绍了如何从javascript调用服务器端功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在线应用程序,我需要调用vb.net(asp.net)函数形式的javascript,这些函数是在拖放事件中触发的。那么如何调用服务器端函数并使用javascript将参数传递给函数....

解决方案

参考这个

http://www.c-sharpcorner.com/ UploadFile / rohatash / calling-server-side-function-from-javascript-in-Asp-Net / [ ^ ]


您可以使用PageMethods从ClientSide(Javascript)调用服务器端功能(C#)



1)向页面添加脚本管理器并设置脚本管理器的EnablePageMethods属性为True。

< asp:ScriptManager ID = ScriptManager2 runat = server EnablePageMethods = true / > ;
2)将Javascript函数添加到页面的正文部分

< body >
< 表单 id = form1 runat = server >
< script type = text / javascript >
函数CallingServerSideFunction(){
PageMethods.GetData();
}
< / script >
< / form >
< / body >
3)在c#

//中添加要调用的方法。请注意,该方法应该是Static和WebMethod。
[System.Web.Services.WebMethod]
public static void GetData()
{
MessageBox.Show(Calling From Client Side);
//你的逻辑来这里
}
4)调用Javascript函数

< < span class =code-leadattribute> input id = Button2 type = 按钮 value = FunctionCall onclick = CallingServerSideFunction() / > ;


I am developing an online app in which i need to call vb.net(asp.net) functions form javascript that are fired on drag and drop events. so how can i call server side functions and pass parameters to the function using javascript....

解决方案

Refer this
http://www.c-sharpcorner.com/UploadFile/rohatash/calling-server-side-function-from-javascript-in-Asp-Net/[^]


You can use the PageMethods to call server side function (C#) from ClientSide(Javascript)



1) Add a script Manager to your page and set the 'EnablePageMethods' property of script manager to True.

<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true"/>
2) Add Javascript function to the body section of page

<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
        function CallingServerSideFunction() {
            PageMethods.GetData();
        }
    </script>
    </form>
</body>
3) Add the method you want to call in c#

    //Please note that the method should be a Static and WebMethod.
    [System.Web.Services.WebMethod]
    public static void GetData()
    {
        MessageBox.Show("Calling From Client Side");
        //Your Logic comes here
    }
4) Calling the Javascript function

<input id="Button2" type="button" value="FunctionCall" onclick="CallingServerSideFunction()" />


这篇关于如何从javascript调用服务器端功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆