如何在无刷新页面的情况下实现客户端回调实例

80酷酷网    80kuku.com

  客户端|刷新|无刷新|页面

经常在网上找各种各样的资料看,来解决某一具有针对性的问题,可是最终发现还是MSDN好,可惜大部分没有汉化,而且实例型的资料并不是很多,但不管怎么说MSDN还是需要我们认真学习的!
<% Page Language="C#" AutoEventWireup="true"
  CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
  1.1//EN" "">

<html xmlns="" >
<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 >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 }

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: