今天读台湾董老师的AJAX书,发现其实也可以不建立asmx这样来调用了
比如
'建立Page Method
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function GetServerTime(ByVal format As String) As String
'调用服务器端时间
Return Now.ToString(format)
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'建立调用后端Web Services的JavaScript
Dim js As String
js = ""
js += "function CallWebServices(){"
js += " PageMethods.GetServerTime('yyyy/MM/dd HH:mm:ss',onOK);"
js += "}"
js += "function onOK(returnData){"
js += " $get('TextBox1').value=returnData;"
js += "}"
js = "<script>" & js & "</script>"
Me.ClientScript.RegisterStartupScript(GetType(String), "", js)
'将调用后端Web Services的JavaScript附加到Button1的onclick事件上
Me.Button1.Attributes("onclick") = "CallWebServices();"
'设置 EnablePageMethods
Me.ScriptManager1.EnablePageMethods = True
End Sub
End Class
前台页面
<form id="form1" runat="server">
<div>
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" runat="server" type="button" value="button" /></div>
</div>
</form>