如何使用javascript(正确)调用方法背后的代码? [英] How can I do a call back to the code behind method using javascript (properly)?

查看:48
本文介绍了如何使用javascript(正确)调用方法背后的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用Javascript回调方法背后的代码,看来唯一的方法是使用

So I am trying to call back to a code behind method using Javascript and it seems like the only way to do so is using the

 Page.ClientScript.RegisterClientScriptBlock()

方法.

我不需要将任何数据返回给正在调用的javascript函数.

I don't need to return any data back to the calling javascript function.

这不是Web服务,因此我将不使用ajax调用,但这似乎比使用Page.ClientScript ......以编程方式进行客户端回调的方法更简单.>

This isn't a web service, so I am not going to use a ajax call, but this seems like there would be an easier way to do this than Client Callbacks Programmatically with Page.ClientScript.....

推荐答案

Page.ClientScript.RegisterClientScriptBlock() 仅用于在页面中推送一些JavaScript代码块.这不是要调用服务器方法.如果要从JavaScript调用服务器代码,则应检查

Page.ClientScript.RegisterClientScriptBlock() is just used to push some JavaScript code block in your page. This is not intended to call server method. If you want to call server code from JavaScript, you should check for the [WebMethod] attribute. Basically, you put that attribute on top of a public static method of your page, and you can call it in JavaScript.

C#示例:

[WebMethod]  
public static void MyWebMethod(string foo)  
{  
    doSomething(foo);  
}

VB.NET示例:

<WebMethod> 
Public Sub MyWebMethod(foo As String)
    doSomething foo
End Function

然后,在JavaScript中:

And then, in JavaScript:

<script type="application/javascript">  
    PageMethods.MyWebMethod(someValue);  
</script>

您已经完成.

这篇关于如何使用javascript(正确)调用方法背后的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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