首先看下ICallbackEventHandler解释:用于指示控件可以作为服务器的回调事件的目标。
它只有两个公共方法
string GetCallbackResult () //返回以控件为目标的回调事件的结果。
void RaiseCallbackEvent (string eventArgument) //处理以控件为目标的回调事件。
新建一个Default.aspx页面:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ICallbackEventHandler Demo</title>
<style type="text/css">
*{font: 12px "verdana";}
#user{border:1px solid #080; height:50px;500px;padding:20px;}
input{border:1px solid #508FCC;background:#FFF;}
.ok{color:#090;}
.bad{color:#F00}
#result{display:inline;margin: 0 5px 0;}
</style>
<script type="text/javascript">
function GetFlag(arg)
{
document.getElementById("result").innerHTML=arg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="user">
<input type="text" value="lhking" maxlength="10" id="userid" onchange="callServer()" />
<input type="button" value="check it!" onclick="callServer()" id="Button1" /><div id="result">查询用户是否被使用。</div>
</div>
</form>
</body>
</html>
Default.aspx.cs代码:
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string _arg = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
string script = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetFlag", "");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callServer", "\nfunction callServer(){\ndocument.getElementById(\"result\").innerHTML=\"正在检查 \"+document.getElementById(\"userid\").value+\" 的可用性,请稍候\";\nvar arg=document.getElementById(\"userid\").value;\n" + script + ";\n}", true);
}
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
System.Threading.Thread.Sleep(1000);
return _arg;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument.Equals("lhking") || eventArgument.Equals("blueidea"))
_arg = string.Format("<span class=\"bad\">很遗憾,{0} 已被使用。</span>", eventArgument);
else
_arg = string.Format("<span class=\"ok\">恭喜您,{0} 可以注册。</span>", eventArgument);
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string _arg = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
string script = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetFlag", "");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callServer", "\nfunction callServer(){\ndocument.getElementById(\"result\").innerHTML=\"正在检查 \"+document.getElementById(\"userid\").value+\" 的可用性,请稍候\";\nvar arg=document.getElementById(\"userid\").value;\n" + script + ";\n}", true);
}
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
System.Threading.Thread.Sleep(1000);
return _arg;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument.Equals("lhking") || eventArgument.Equals("blueidea"))
_arg = string.Format("<span class=\"bad\">很遗憾,{0} 已被使用。</span>", eventArgument);
else
_arg = string.Format("<span class=\"ok\">恭喜您,{0} 可以注册。</span>", eventArgument);
}
#endregion
}