利用ICallbackEventHandle實作類別似AJAX的無重新整理頁面

來源:互聯網
上載者:User
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" /><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的無重新整理頁面,而且實現起來十分簡單。

http://pyw0818.cnblogs.com/archive/2006/05/30/413416.html



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.