前端:
<script type="text/javascript">
function ReceiveServerData(rValue)
{
document.getElementById("ResultsSpan").innerHTML = rValue;
}
</script>
後端:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
public partial class Default2 : System.Web.UI.Page,ICallbackEventHandler
{
protected ListDictionary catalog;
protected string returnValue;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "document.getElementById('" + ListBox1.ClientID + "').options[document.getElementById('" + ListBox1.ClientID + "').selectedIndex].text", "ReceiveServerData", "");
ListBox1.Attributes.Add("onchange", cbReference);
catalog = new ListDictionary();
catalog.Add("螢幕", 42);
catalog.Add("筆記型電腦", 10);
catalog.Add("鍵盤", 23);
catalog.Add("滑鼠", 17);
catalog.Add("桌上型電腦", 37);
catalog.Add("硬碟", 76);
catalog.Add("燒錄機", 34);
catalog.Add("電源供應器", 21);
ListBox1.DataSource = catalog;
ListBox1.DataTextField = "key";
ListBox1.DataBind();
}
string ICallbackEventHandler.GetCallbackResult()
{
return returnValue;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
if (catalog[eventArgument] == null)
{
returnValue = "-1";
}
else
{
returnValue = catalog[eventArgument].ToString();
}
}
}