从javascript调用页面方法 [英] Calling page methods from javascript

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

问题描述

大家好,

我想从javascript调用页面方法。如何通过加载页面调用pagemethods。

Hi all,
I want calling page methods from javascript. how to call pagemethods with loading page.

推荐答案

你需要像这样制作你的页面方法...

确保它会静态公共标识符。

you need to make your page method like this...
make sure it will static with public identifier.
[WebMethod]
public static void YouPageMethod()
{

}









如果您想在页面加载事件上调用该方法,那么您需要像这样调用它。 ...







and if you want to call that method on your page load event then you need to call it like this....

<script type="text/javascript">


(文件).ready(function(){
(document).ready(function () {


.ajax({
typ e:POST,
url:Default.aspx / YouPageMethod,
data:,
contentType:application / json; charset = utf-8,
dataType:json,
成功:函数(结果){
alert('方法称为完全成功');
},
错误:函数(结果){
alert(错误+结果);
}
});
});
< / script >
.ajax({ type: "POST", url: "Default.aspx/YouPageMethod", data: "", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert('Method Called Sucess fully'); }, error: function (result) { alert("error " + result); } }); }); </script>







如果你需要将参数传递给你的方法然后使用这样的数据,但有一点参数名称与你的方法参数相同name。




if you need to pass parameter to your method then use data like this, but one thing parameter name is same as your method parameter name.

data:"{ParamterName:'VALUE',ParamterName2:'Value'}",





但还有一件事你不能访问你的表单控件,比如grid或者您的Web方法旁边的文本框,因为它是静态的。



but one more thing you can not access your form control like grid or text box in side your web method as it is static.


这篇关于从javascript调用页面方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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