惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - Teracy

扫站:比较购物网站列表 MVC 2.0 学习笔记 和 Demo共享 网络书签的代码整理 2009年12月的工作发现 电子商务的认知 招聘Net软件工程师,网站设计师 sql2005数据库sa密码忘记了,该怎么办? 08年最后一天该说点什么了 邀请大家谈谈山寨版 工作小结 + 发布新的邮件发送工具 在ASP.NET中自动给URL地址加上超链接 把搜索引擎营销变成你的企业营销绩效飞跃重要策略 Google搜索引擎排名技巧 在ASP.NET中显示进度条-ASP.NET 工作记录 上班时间也要忙里偷闲 项目小结+Repeater中如何使用单选按钮 招聘Net软件工程师,测试工程师: 招聘ASP.NET技术人员
ASP.NET2.0实现无刷新客户端回调(热点技术)
Teracy · 2008-12-06 · via 博客园 - Teracy

前言:  很久没有写东西了,最近忙着自己的技术网站的发布. 希望我的技术网能给大家的工作带来更多的帮助.  www.yy0808.com

Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面。

  首先我们要说一个很重要的方法:GetCallbackEventRefernce.我把我的理解写出来,可能是错误的,恳请指出,非常感谢!

  GetCallbackEventReference首先实现让客户端脚本有能力传递参数给服务器端的RaiseCallbackEvent方法,然后返回RaiseCallBackEvent方法的值给你在GetCallbackEventRefernce方法中注册的一个参数(其实也是一个你要在客户端写的脚本)。调用GetCallbackEventRefernce你必须从客户端脚本中传递给他两个参数,一个是要传递给RaiseCallbackEvent事件的值,一个是context.

  他的参数意义如下:

  第一个:实现了ICallbackEventHandler借口的页面或者服务器控件,写this代表但前页面。

  第二个:代表你从要从客户端传递给服务器RaiseCallbackEvent方法的值

  第三个:你要在客户端写的一个js函数,同时,服务器也会把计算得到的数据传递给这个函数做为这个函数的参数。

  第四个:context具体什么意思我也不太清楚GetCallbackEventRefernce发送到了客户、端的代码是这样的:

WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false) 

  那么我们要怎么样做才能够从客户端调用他呢?看到了三中方法:

  第一种:在后台写个public string,在Page_Load中给他赋值为:=Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");注意在这里是Page.ClientScrip,因为他会返回个ClientScriptManager,ClientScriptManager管理所有的客户端脚本。然后在前台某个按钮的onclick事件里<%=那个public后台字符串%>.做个小实验代码如下:

  前台ServerTime.aspx:为了方便去掉好多没用的html

<%@ page language="C#" CodeFile="ServerTime.aspx.cs" Inherits="ServerTime_aspx" %>
<html>
<head>
<title>Server Time</title>
<script language="javascript">

function GetServerTime()
{
 var message = '';
 var context = '';
 <%=sCallBackFunctionInvocation%>
}

function ShowServerTime(timeMessage, context) {
 alert('现在服务器上的时间是:\n' + timeMessage);
}
</script>
</head>
<body>
<form id="MainForm" runat="server">
<input type="button" value="得到服务器端时间" onclick="GetServerTime();" />
</form>
</body>
</html> 

  后台:

using System;
using System.Web.UI;

public partial class ServerTime_aspx : Page,ICallbackEventHandler
{
 //一定要实现ICallbackEventHandler借口
 public string sCallBackFunctionInvocation;

 void Page_Load(object sender, System.EventArgs e)
 {
  sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");
 }

 public string RaiseCallbackEvent(string eventArgument)
 {
  return DateTime.Now.ToString();
 }
}

  运行,点按钮结果如下:

  第二种方法:在上面的方法中我们必须要在前台绑定后台,那么如果不绑定呢?我们这样做:

  直接把GetCallbackEventReference当做js函数中的一个实现内容,然后把这个js函数注册到客户端。

  前台TestPage代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function test()
{
 var lb = document.getElementById("Select1");
 //取的那个下拉框
 var con = lb.options[lb.selectedIndex].text;
 //得到你选择的下拉框的文本再调用呢个CallTheServer,是一个由服务器端输出的js函数
 CallTheServer(con,'');
}
function ReceiveServerData(rValue)
{
 Results.innerHTML = rValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<option value=1 selected="selected">老鼠徒弟</option>
<option value=2>吴旗娃师傅</option>
</select>
<br />
<br />
<input onclick="test()" value="从服务器返回下拉框文本" type=button>
<br />
<br />
<span ID="Results"></span>
<br />
</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 TestPage : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
 protected void Page_Load(object sender, EventArgs e)
 {
  String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
  String callbackScript;
  callbackScript = "function CallTheServer(arg,context)" +"{ " + cbReference + "} ;";
  Page.ClientScript.RegisterStartupScript(this.GetType(),"abcdefg",callbackScript, true);
  //第四个参数代表是不是要自动给着脚本加上<script type="text/javascript"></script>标记,当然要加啊
 }
 public String RaiseCallbackEvent(String eventArgument)
 {
  return "你选择的是" + eventArgument;
 }
}

  下面是执行结果:

  第三种:前面两种都是<input type="button"的html控件,那么如果是服务器按钮呢?当然也可以,在后台添加服务器按钮的onclick 属性。

  前台third.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="third.aspx.cs" Inherits="third" %>
<html>
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<option selected="selected" value=1>老鼠徒弟</option>
<option value=2>吴旗娃师傅</option>
</select>
<asp:Button ID="Button1" runat="server" Text="这是个服务器按钮" /></div>
<div id="div1" />
<script type="text/javascript">
function Re(ret)
{
 document.getElementById("div1").innerHTML = ret;
 alert(ret);
}
</script>
</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 third : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
 protected void Page_Load(object sender, EventArgs e)
 {
  //第四个参数为null,因为你不可能再在js中给他传参数了
  string str = Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('Select1')._
       options[document.getElementById('Select1').selectedIndex].text","Re",null);
  //return false是为了防止提交窗体
  Button1.Attributes.Add("onclick",str+";return false;");
 }

 #region ICallbackEventHandler Members
 
 public string RaiseCallbackEvent(string eventArgument)
 {
  if (eventArgument == "老鼠徒弟")
  {
   return "老鼠徒弟:人生如鼠,不在仓就在厕!";
  }
  else
  {
   return "吴旗娃师傅:自信自强,乐观向上";
  }
 }
 #endregion
}
 

  小技巧,当你写完System.Web.UI.ICallbackEventHandler后,把鼠标移上去,那么System前面会有个小图表,点他会自动写好那个RaiseCallbackEvent代码,效果如下;