如何在無重新整理頁面的情況下實現用戶端回調執行個體(C#)—MSDN學習筆記

來源:互聯網
上載者:User
經常在網上找各種各樣的資料看,來解決某一具有針對性的問題,可是最終發現還是MSDN好,可惜大部分沒有漢化,而且執行個體型的資料並不是很多,但不管怎麼說MSDN還是需要我們認真學習的!<%@ 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 runat="server">
  <script type="text/javascript">
    function LookUpStock()
    {
        var lb = document.forms[0].ListBox1;
        var product = lb.options[lb.selectedIndex].text 
        CallServer(product, "");
    }
    
    function ReceiveServerData(rValue)
    {
        Results.innerText = rValue;
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      <br />
      <br />
      <button onclick="LookUpStock()">Look Up Stock</button>
      <br />
      <br />
      Items in stock: <span ID="Results"></span>
      <br />
    </div>
  </form>
</body>
</html> 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Collections;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 
12 public partial class ClientCallback : System.Web.UI.Page,
13      System.Web.UI.ICallbackEventHandler
14 {
15     protected System.Collections.Specialized.ListDictionary catalog;
16     protected void Page_Load(object sender, EventArgs e)
17     {
18         String cbReference =
19             Page.ClientScript.GetCallbackEventReference(this,
20             "arg", "ReceiveServerData", "context");
21         String callbackScript;
22         callbackScript = "function CallServer(arg, context)" +
23             "{ " + cbReference + "} ;";
24         Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
25             "CallServer", callbackScript, true);
26 
27         catalog = new System.Collections.Specialized.ListDictionary();
28         catalog.Add("monitor", 12);
29         catalog.Add("laptop", 10);
30         catalog.Add("keyboard", 23);
31         catalog.Add("mouse", 17);
32 
33         ListBox1.DataSource = catalog;
34         ListBox1.DataTextField = "key";
35         ListBox1.DataBind();
36     }
37 
38     public String RaiseCallbackEvent(String eventArgument)
39     {
40         String returnValue;
41         if (catalog[eventArgument] == null)
42         {
43             returnValue = "-1";
44         }
45         else
46         {
47             returnValue = catalog[eventArgument].ToString();
48         }
49         return returnValue;
50     }
51 }

相關文章

聯繫我們

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