日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式

發布時間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

頁面無刷新回發實現有很多種方式,可以用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這個方法里寫頁面處理代碼即可。

是不是很簡單?:)

轉載于:https://www.cnblogs.com/myself/archive/2010/03/29/1699737.html

總結

以上是生活随笔為你收集整理的社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。