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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - o0myself0o

利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现 生产者消费者模式,代码中碰到的疑问(已解决) 利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式 Entity Framework 4.0 ObjectContext下的各种方法实践 题目:若干个不重复数,打乱顺序输出 wtf js(四) - o0myself0o - 博客园 wtf js(三) number的类型不是number wtf js(二) 算法:给定两个已从小到大排好序的整型数组arrA和arrB,将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序 应用中的单例模式 面试题:给你三个bool类型变量a, b, c,判断至少有两个为true javascript面向对象编程(一) - o0myself0o - 博客园 wtf js(一) - o0myself0o - 博客园 社区网站功能实现系列(二):社区内容分享到别的SNS 社区网站功能实现系列(一):多国语言的实现 反射获取Class中Property的值 A*寻路初探 闲谈ASP.NET 2.0缓存技术 使用 jQuery 简化 Ajax 开发
社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式
o0myself0o · 2010-03-29 · via 博客园 - o0myself0o

页面无刷新回发实现有很多种方式,可以用XMLHttpRequest,一些js框架的ajax实现(如jQuery的 ajax),ajaxPro,MS的UpdatePanel,web服务等。下面我来介绍另一种方式:页面回发。

1. ICallbackEventHandler接口
该接口用于指示控件可以作为服务器的回调事件的目标。ICallbackEventHandler 接口的控件为目标时,将把事件变量作为 参数传递来调用 RaiseCallbackEvent 方法以处理该事件,并且 GetCallbackResult 方法返回回调的结果。继承这个接口需要实现两个方法: RaiseCallbackEvent, GetCallbackResult .

public void RaiseCallbackEvent(string eventArgument)

{}

这个方法用于处理客户端提交的请求。它接收一个string类型的参数,这个参数由客户端传入。如果你需要传入多个参数,你可以将这些参数组合起 来,然后到这个方法里面再分解。

public string GetCallbackResult()

{}

该方法用于返回服务器端处理后的数据。如果你要返回的数据类型不仅仅是string类型,你可以自己构造一个类,返回的时候通过序列化这个类达到返 回复杂类型的效果。客户端获取的时候用eval("(string)")来反序列化即可。

2. 方法GetCallbackEventReference()。用于向服务器端发送回调后请求的函数。

public string GetCallbackEventReference(Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync){}

看参数列表的名字就知道大概什 么意思了,不赘述。

先参考代码:

前台页面:

<html><head runat="server">
<title>无标题页</title>
<script type="text/javascript">

function ReceiveCallback(result)
{
var resultArrays = eval("(" + result + ")");

alert(resultArrays.RepeaterJson);
}

function ChangeGameTab(order)
{
CallServer(order, null);
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div class="">
<div class="">
<h3>网页游戏列表</h3>
<a class="z1 tj00" id="tj00"  href="javascript:ChangeGameTab('1');">最新游戏</a>
<a id="tj01" class=""  href="javascript:ChangeGameTab('2');">策略战争</a>
<a id="tj02" class=""  href="javascript:ChangeGameTab('3');">角色扮演</a>
<a id="tj03" class=""  href="javascript:ChangeGameTab('4');">棋牌类</a>
<a id="tj04" class=""  href="javascript:ChangeGameTab('5');">战略养成类</a>
</div>
<ul id="t1">
<uc1:GameList runat="server" id="GameList1" CategoryID="1"></uc1:GameList>
</ul>
<ul style="display: none;" id="t2"></ul>
<ul style="display: none;" id="t3"></ul>
<ul style="display: none;" id="t4"></ul>
<ul style="display: none;" id="t5"></ul>
<div></div>
</div>
</form>
</body>
</html>

后台代码:

namespace Game
{
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.RegistCallbackScript(this.Page);
}

if (Page.IsPostBack && !Page.IsCallback)
{
this.RegistCallbackScript(this.Page);
}
}

#region ICallbackEventHandler 成员

public string GetCallbackResult()
{
Game.Entities.GameRepeaterJson gameJson = new Game.Entities.GameRepeaterJson();
gameJson.RepeaterJson = this.RewriteControl(this.GameList1);
return SNET.Common.DataAccessHelper.JSONHelper.ObjectToString(gameJson);
}

public void RaiseCallbackEvent(string eventArgument)
{
switch (eventArgument)
{
case "1":
this.GameList1.CategoryID = "1";
this.GameList1.RPTBind();
break;
case "2":
this.GameList1.CategoryID = "3";
this.GameList1.RPTBind();
break;
case "3":
this.GameList1.CategoryID = "4";
this.GameList1.RPTBind();
break;
case "4":
this.GameList1.CategoryID = "6";
this.GameList1.RPTBind();
break;
case "5":
this.GameList1.CategoryID = "8";
this.GameList1.RPTBind();
break;
default:
this.GameList1.CategoryID = "1";
this.GameList1.RPTBind();
break;
}
}

#endregion

/// <summary>
/// 注册无刷新回调事件
/// </summary>
/// <param name="page">The page.</param>
protected void RegistCallbackScript(System.Web.UI.Page page)
{
string callbackReference = page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveCallback", "context",null, false);
string callbackScript = string.Format("function CallServer(arg,context){{ {0} }}", callbackReference);
page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}

/// <summary>
/// 获取指定控件重画的内容
/// </summary>
/// <param name="control">The control.</param>
/// <returns></returns>
protected string RewriteControl(Control control)
{
StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
control.RenderControl(htmlTextWriter);
htmlTextWriter.Flush();
htmlTextWriter.Close();
return stringWriter.ToString();
}
}
}

Game.Entities.GameRepeaterJson 是我自定义的一个类,为了说明可以返回复杂类型。

这样就实现了客户端向服务器端 请求数据,而页面无刷新了。

代码关键是RegistCallbackScript 这个方法,它向页面注册了一个function,同时也说明了它的回调函数是ReceiveCallback, 继而你只要在页面上的ReceiveCallback这个方法里写页面处理代码即可。

是不是很简单?:)