代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 <script language="javascript" type="text/javascript">
9 function success(args, context) {
10 document.getElementById("sp").innerHTML = args;
11 }
12 function errors(args, context) {
13 document.getElementById("sp").innerHTML = "加载出现了异常";
14 }
15 </script>
16 </head>
17 <body>
18 <form id="form1" runat="server">
19 <asp:Label ID="lbName" runat="server" Text="用户名:"></asp:Label>
20 <asp:TextBox ID="txtName" runat="server" onblur="CallServerMonthed(txtName.value,null)"></asp:TextBox>
21 <span id="sp"></span>
22 </form>
23 </body>
24 </html>
25
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class _Default : System.Web.UI.Page,ICallbackEventHandler
9 {
10 protected string sResult = "";
11 protected void Page_Load(object sender, EventArgs e)
12 {
13 //获取ClientScriptManager的客户端引用
14 ClientScriptManager csm = Page.ClientScript;
15 //GetCallbackEventReference(当前的类,参数,成功是执行的方法,"",失败时执行的方法,同步(false)/异步(true))
16 string reference = csm.GetCallbackEventReference(this,"args","success","","errors",true);
17 string callbackScript = "function CallServerMonthed(args,context){\n"+reference+"}";
18 csm.RegisterStartupScript(this.GetType(), "CallServerMonthed", callbackScript, true);
19 }
20
21 #region ICallbackEventHandler Members
22
23 public string GetCallbackResult()
24 {
25 return sResult;
26 }
27
28 public void RaiseCallbackEvent(string eventArgument)
29 {
30 if (eventArgument.ToLower().IndexOf("admin") != -1)
31 {
32 sResult = "不能注册";
33 }
34 else
35 {
36 sResult = "可以注册";
37 }
38 }
39
40 #endregion
41 }
42
.net自带的回调技术,没有用ajax来实现,注意在.cs服务器端要继承ICallbackEventHandler接口,
ICallbackEventHandler:
用于指示控件可以作为服务器的回调事件的目标。
命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)