用戶端回調實現 (C#) 樣本

來源:互聯網
上載者:User

示範實現用戶端回調的 ASP.NET 網頁。有關更多資訊,請參見在 ASP.NET 網頁中不經過回傳而以編程方式實現用戶端回調。

樣本說明

下面的程式碼範例分為兩部分。樣本的第一部分示範一個 ASP.NET 網頁(.aspx 頁)。第二部分示範相應的程式碼後置檔案(.aspx.cs 檔案)。

代碼

C#  複製代碼
<%@ 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/xhtml" >            <head id="Head1" runat="server">            <title>Client Callback Example</title>            <script type="text/ecmascript">            function LookUpStock()            {            var lb = document.getElementById("ListBox1");            var product = lb.options[lb.selectedIndex].text;            CallServer(product, "");            }            function ReceiveServerData(rValue)            {            document.getElementById("ResultsSpan").innerHTML = rValue;            }            </script>            </head>            <body>            <form id="form1" runat="server">            <div>            <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>            <br />            <br />            <button type="Button" onclick="LookUpStock()">Look Up Stock</button>            <br />            <br />            Items in stock: <span id="ResultsSpan" runat="server"></span>            <br />            </div>            </form>            </body>            </html>            

C#  複製代碼
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.ICallbackEventHandler            {            protected System.Collections.Specialized.ListDictionary catalog;            protected String returnValue;            protected void Page_Load(object sender, EventArgs e)            {            String cbReference =            Page.ClientScript.GetCallbackEventReference(this,            "arg", "ReceiveServerData", "context");            String callbackScript;            callbackScript = "function CallServer(arg, context)" +            "{ " + cbReference + ";}";            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),            "CallServer", callbackScript, true);            catalog = new System.Collections.Specialized.ListDictionary();            catalog.Add("monitor", 12);            catalog.Add("laptop", 10);            catalog.Add("keyboard", 23);            catalog.Add("mouse", 17);            ListBox1.DataSource = catalog;            ListBox1.DataTextField = "key";            ListBox1.DataBind();            }            public void RaiseCallbackEvent(String eventArgument)            {            if (catalog[eventArgument] == null)            {            returnValue = "-1";            }            else            {            returnValue = catalog[eventArgument].ToString();            }            }            public String GetCallbackResult()            {            return returnValue;            }            }            

注釋

該網頁類比一個資料庫尋找,以確定一系列產品(監視器、鍵盤等)的供貨數量或庫存數量。為了簡化此程式碼範例,資料庫由包含少量物品的詞典列表來表示。對於表中的每件物品,鍵就是物品名稱(如監視器),值就是物品的庫存數。但是在成品應用程式中,將使用資料庫。

當運行此頁時, 控制項被綁定到雜湊表,這樣, 控制項便可以顯示產品列表。此頁還包含一個 button 元素(非 Button Web 伺服器控制項),其 onclick 事件被綁定到一個名為 LookUpStock 的用戶端函數。當使用者單擊按鈕時,該按鈕便會執行 LookUpStock 函數,此函數從列表框中擷取當前所選內容,然後通過調用 CallServer 函數來執行用戶端回調。

程式碼後置頁通過 方法向該頁添加用戶端指令碼。添加到該頁的指令碼包括一個稱為 CallServer 的函數,此函數用於擷取將從 方法回傳到伺服器的方法的名稱。

用戶端回調會調用 方法,以確定傳遞給它的產品的可用庫存。 方法將返回該值。請注意,在用戶端指令碼與伺服器代碼之間發送的參數只能是字串。若要傳入或接收多個值,可以分別在輸入字串或返回字串中將這些值串聯起來。

安全記事:

使用此功能時,存在潛在的安全威脅。由於不對回調參數進行驗證,因此存在一定的不安全因素。每次使用參數之前,都應對參數的內容進行檢查。有關詳細資料,請參見指令碼侵入概述。

請參見概念在 ASP.NET 網頁中不經過回傳而以編程方式實現用戶端回調如何:在 ASP.NET 網頁中實現回調具有驗證實現的用戶端回調樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.