javascript中调用后台的方法,就是实现无刷新改变文本框中的值。首先要前台页面中 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>加入EnablePageMethods="true",
同时,在后台定义方法时如下:
[System.Web.Services.WebMethod]
public static string Foo(string name)
{
return "hello";
}
页面如下:
<script language="javascript">
function win() {
PageMethods.Foo("aa", myResult, funError);//"aa"为Foo方法中的参数值
}
function myResult(temp) {
document.getElementById("txt1").value = temp;
}
function funError(err) {
alert("Error:" + err._message);
}
</script>
<input id="Button1" type="button" value="button" οnclick="win()" />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>