.Net中JS调用后台函数
例:后台函数
Public Shared Function GetHelloMessage(ByVal str As String) As String
Return "Hello " + str
End Function
一、直接调用
var b="world";
var a='<%=GetHelloMessage("'+b+'")%>';
alert(a);
返回值:Hello World
二、PageMethods
实现步骤:在页面上添加ScriptManager控件,将EnablePageMethods属性设为true,切换到代码视图,在函数GetHelloMessage前面加上
<System.Web.Services.WebMethod()> _
前台调用:
<script>
function hellomsg()
{
var b="World";
PageMethods.GetHelloMessage(b,OnSucceeded,OnFailed);
}
//成功返回
function OnSucceeded(result,userContext,methodName)
{
if(methodName=="GetHelloMessage")
{alert(result);}
}
//失败返回
function OnFailed(error,userContext,methodName)
{
if(error!==null)
{alert(error.get_message());}
}
</script>