首页 > 开发 > 综合 > 正文

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

2024-07-21 02:28:46
字体:
来源:转载
供稿:网友

经常在网上找各种各样的资料看,来解决某一具有针对性的问题,可是最终发现还是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 >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 }

最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表