<span class="t18"><P>经常在网上找各种各样的资料看,来解决某一具有针对性的问题,可是最终发现还是MSDN好,可惜大部分没有汉化,而且实例型的资料并不是很多,但不管怎么说MSDN还是需要我们认真学习的!<BR><%@ Page Language="C#" AutoEventWireup="true" <BR> CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %></P>
<P><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML <BR> 1.1//EN" "<A href="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd</A>"></P>
<P><html xmlns="<A href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</A>" ><BR><head runat="server"><BR> <script type="text/javascript"><BR> function LookUpStock()<BR> {<BR> var lb = document.forms[0].ListBox1;<BR> var product = lb.options[lb.selectedIndex].text <BR> CallServer(product, "");<BR> }<BR> <BR> function ReceiveServerData(rValue)<BR> {<BR> Results.innerText = rValue;<BR> }<BR> </script><BR></head><BR><body><BR> <form id="form1" runat="server"><BR> <div><BR> <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox><BR> <br /><BR> <br /><BR> <button onclick="LookUpStock()">Look Up Stock</button><BR> <br /><BR> <br /><BR> Items in stock: <span ID="Results"></span><BR> <br /><BR> </div><BR> </form><BR></body><BR></html></P>
<P> 1 using System;<BR> 2 using System.Data;<BR> 3 using System.Configuration;<BR> 4 using System.Collections;<BR> 5 using System.Web;<BR> 6 using System.Web.Security;<BR> 7 using System.Web.UI;<BR> 8 using System.Web.UI.WebControls;<BR> 9 using System.Web.UI.WebControls.WebParts;<BR>10 using System.Web.UI.HtmlControls;<BR>11 <BR>12 public partial class ClientCallback : System.Web.UI.Page,<BR>13 System.Web.UI.ICallbackEventHandler<BR>14 {<BR>15 protected System.Collections.Specialized.ListDictionary catalog;<BR>16 protected void Page_Load(object sender, EventArgs e)<BR>17 {<BR>18 String cbReference =<BR>19 Page.ClientScript.GetCallbackEventReference(this,<BR>20 "arg", "ReceiveServerData", "context");<BR>21 String callbackScript;<BR>22 callbackScript = "function CallServer(arg, context)" +<BR>23 "{ " + cbReference + "} ;";<BR>24 Page.ClientScript.RegisterClientScriptBlock(this.GetType(),<BR>25 "CallServer", callbackScript, true);<BR>26 <BR>27 catalog = new System.Collections.Specialized.ListDictionary();<BR>28 catalog.Add("monitor", 12);<BR>29 catalog.Add("laptop", 10);<BR>30 catalog.Add("keyboard", 23);<BR>31 catalog.Add("mouse", 17);<BR>32 <BR>33 ListBox1.DataSource = catalog;<BR>34 ListBox1.DataTextField = "key";<BR>35 ListBox1.DataBind();<BR>36 }<BR>37 <BR>38 public String RaiseCallbackEvent(String eventArgument)<BR>39 {<BR>40 String returnValue;<BR>41 if (catalog[eventArgument] == null)<BR>42 {<BR>43 returnValue = "-1";<BR>44 }<BR>45 else<BR>46 {<BR>47 returnValue = catalog[eventArgument].ToString();<BR>48 }<BR>49 return returnValue;<BR>50 }<BR>51 }</P></span>