今天,为了实现在用户焦点离开一个输入框的时候,能够从数据库查询相应的信息,并且填如相应的输入框中,就简单用了一下这个Ajax框架。碰到了不少问题,也Google了不少时间,才终于成功的运行起来了。
首先,给输入框添加焦点失去的事件。我选择用OnBlur,不知道是不是最正确的,^_^。
1 onblur="javascript:CheckName()"
然后在CheckName中,获取输入的输入框的值,把它传到服务器端定义的事件中,并且定义回调事件1 function CheckName()
2 {
3 var name = document.getElementById("xxx").value;
4 PageMethods.GetLotInfo(name, CheckResult);
5 }
6
7 function CheckResult(result)
8 {
9 }
2 {
3 var name = document.getElementById("xxx").value;
4 PageMethods.GetLotInfo(name, CheckResult);
5 }
6
7 function CheckResult(result)
8 {
9 }
1 [System.Web.Services.WebMethod]
2 [System.Web.Script.Services.ScriptMethod]
3 public static string GetLotInfo(string name)
4 {
5 //


return something.
6 }
7
8 //在Asp.Net Ajax正式发布前,ScriptMethod的命名空间 为[Microsoft.Web.Script.Services.ScriptMethod]
2 [System.Web.Script.Services.ScriptMethod]
3 public static string GetLotInfo(string name)
4 {
5 //
return something.
6 }
7
8 //在Asp.Net Ajax正式发布前,ScriptMethod的命名空间 为[Microsoft.Web.Script.Services.ScriptMethod]
注:为了能引用System.Web.Script.Service.ScriptMethod, 需要引入System.Web.Extensions.dll