DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 利用ICallbackEventHandle實現類似AJAX的無刷新頁面
利用ICallbackEventHandle實現類似AJAX的無刷新頁面
編輯:AJAX詳解     

先看MSDN Library 2005上的這個例子!
------ ClIEntCallback.ASPx ------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClIEntCallback.ASPx.cs" ­ Inherits="ClIEntCallback" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/­ xHtml11.dtd">

<Html XMLns="http://www.w3.org/1999/xhtm­ l" >
    <head id="Head1" runat="server">
        <title>Callback Test</title>
        <script type="text/Javascript">
        function ReceiveServerData(receivedStr, context)
        {
            alert(receivedStr);
        }
        </script>
    </head>
        <body>
          <form id="form1" runat="server">
            <input type="button" value="Callback" onclick="CallServer('argument', 'context')"/><br />
          </form>
    </body>
</Html>-------- ClIEntCallbacp.ASPx.cs  -----------
// ClIEntCallback.ASPx.cs
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 ClIEntCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandle­ r
{

    void Page_Load(object sender, EventArgs e)
    {
        ClientScriptManager cm = Page.ClIEntScript;
        String cbReference = cm.GetCallbackEventReference(this,­ "arg", "ReceiveServerData", "");
        String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
        cm.RegisterClIEntScriptBlock(this.­ GetType(), "CallServer", callbackScript, true);
    }

    private string returnStr;
    //function called by clIEnt, executed on server
    public void RaiseCallbackEvent(String eventArgument)   
    {
        //do something with return argument
        returnStr = eventArgument.ToUpper();
        return;
    }
   
    //function that sends result?
    public string GetCallbackResult()
    {
        return returnStr;
    }
}
客戶端用ReceiveServerData接收服務器返回的數據,
<script type="text/Javascript">
        function ReceiveServerData(receivedStr, context)
        {
            alert(receivedStr);
        }
 </script>

使用CallServer('argument', 'context')傳遞數據到服務器。

客戶端ok了,接下來是服務器端,
實現ICallbackEventHandle­ 接口,
private string returnStr;
 public void RaiseCallbackEvent(String eventArgument)   
    {
        returnStr = eventArgument.ToUpper();
        return;
    }
   
public string GetCallbackResult()
    {
        return returnStr;
    }

最後,將客戶端方法和服務器端的方法關聯起來,在Page_Load中實現,
ClientScriptManager cm = Page.ClIEntScript;
 String cbReference = cm.GetCallbackEventReference(this,­ "arg", "ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
 cm.RegisterClIEntScriptBlock(this.­ GetType(), "CallServer", callbackScript, true);

大功告成!

這種方法也能達到類似AJax的無刷新頁面,而且實現起來十分簡單。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved