职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 938|回复: 0

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

[复制链接]
joe 发表于 2007-2-10 19:55 | 显示全部楼层 |阅读模式
经常在网上找各种各样的资料看,来解决某一具有针对性的问题,可是最终发现还是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>
      <aspistBox ID=\"ListBox1\" Runat=\"server\"></aspistBox>
      <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 }
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-14 07:25 , Processed in 0.124214 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表