学习文档:http://download.csdn.net/source/899736
实例一:
前台
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckName.aspx.cs" Inherits="CallBack_CheckName" %>
- <!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>判断用户名是否被占用</title>
- <script type="text/javascript">
- function CallServer(inputcontrol,context)
- {
- context.innerHTML = "加载中......";
- arg = inputcontrol.value;
- <%= ClientScript.GetCallbackEventReference(this, "arg", "OnCallBack", null) %>;
- }
- function OnCallBack(result,context)
- {
- //context.innerHTML = ""; //当引用为null时,这里不可用.
- lblShow.innerHTML = "";
- alert(result);
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:textbox ID="txtEnter" runat="server"></asp:textbox><br />
- <input id="btnSubmit" type="button" value="submit" onclick="CallServer(txtEnter,lblShow);" /><br />
- <asp:label ID="lblShow" runat="server" Text=""></asp:label>
- </div>
- </form>
- </body>
- </html>
后台
- 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 CallBack_CheckName : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
- {
- private string result;
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //引发回调事件处理
- public void RaiseCallbackEvent(string eventArgument)
- {
- if (eventArgument == "greatverve")
- result = "被占用,请改一个!";
- else
- result = "可以使用!";
- }
- //回传回调结果
- public string GetCallbackResult()
- {
- return result;
- }
- }
实例二:
前台
- <%@ page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CallBack_Default" %>
- <!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>客户端回调</title>
- <script type="text/javascript">
- function CallServer(inputcontrol,context)
- {
- //回调还没有处理完全时其预先加载的显示值
- context.innerHTML = "加载中......";
- //为你在文本框中输入的信息,并且arg在这里就是将其值传递到
- //RaiseCallbackEvent(String eventArgument)方法对应的eventArgument中
- arg = inputcontrol.value;
- //获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调。
- //第一个参数:表示当前页;第二个参数:发向服务器的string;
- //第三个参数:回调函数; 第四个参数:
- <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context") %>;
- }
- //这个是回调的函数
- //这个函数只是提供必要的参数.真正的回调是在服务器端执行.
- //格式固定,第一个参数是返回的数据,第二个参数是引用的id
- function ReceiveServerData(result,context)
- {
- context.innerHTML = result;
- lbl_show.innerHTML = result;
- alert(result);
- }
- //如果传入的参数是服务控件的id,就可以不用getElementByID();
- //非服务器控件也可以直接取,呵呵.
- function f(serverControl){
- alert(serverControl.value);
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:textbox ID="txtEnter" runat="server"></asp:textbox><br />
- <input id="btnSubmit" type="button" value="submit" onclick="CallServer(txtEnter,lblShow);" /><br />
- <asp:label ID="lblShow" runat="server" Text="Label"></asp:label>
- </div>
- <div>
- <asp:label ID="lbl_show" runat="server" Text="Label"></asp:label>
- <input type="button" value="取得服务器控件" onclick="f(txtEnter);" /><br />
- <input id="txtInput" type="text" /><br />
- <input type="button" value="submit" onclick="f(txtInput);" />
- </div>
- </form>
- </body>
- </html>
后台
- 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 CallBack_Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
- {
- //定义一个字符串,回调的结果信息将保存在该字符串中
- private string result;
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //引发回调事件处理
- public void RaiseCallbackEvent(string eventArgument)
- {
- //"eventArgument"为从客户端的JavaScript传递过来的参数
- result = "从服务器端返回的内容:" + eventArgument;
- }
- //回传回调结果
- public string GetCallbackResult()
- {
- return result;
- }
- }
end