<head id="Head1" runat="server"> <title>Client Callback Example</title> <script type="text/ecmascript"> function LookUpStock() { var lb = document.getElementById("ListBox1"); try { var product = lb.options[lb.selectedIndex].text; CallServer(product, ""); } catch(err) { alert("Please select a product."); } } function ReceiveServerData(rValue) { document.getElementById("ResultsSpan").innerHTML = rValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ListBox ID="ListBox1" Runat="server"> <br /> <br /> <input type="button" value="Look Up Stock" id="LookUpButton" önclick="LookUpStock()" /> <br /> <br /> Items in stock: <span id="ResultsSpan" runat="server"> <br /> </div> </form> </body>
public partial class ClientCallback : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler { protected System.Collections.Specialized.ListDictionary catalog; protected String returnValue; protected void Page_Load(object sender, EventArgs e) { string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg","ReceiveServerData","context"); string callbackScript; callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + ";}"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true); catalog = new System.Collections.Specialized.ListDictionary(); catalog.Add("monitor", 12); catalog.Add("laptop", 10); catalog.Add("keyboard", 23); catalog.Add("mouse", 17); ListBox1.DataSource = catalog; ListBox1.DataTextField = "key"; ListBox1.DataBind(); } public void RaiseCallbackEvent(String eventArgument) { if (catalog[eventArgument] == null) { returnValue = "-1"; } else { returnValue = catalog[eventArgument].ToString(); } } public String GetCallbackResult() { return returnValue; } }
ControlID.Attributes.Add("javascript: onclick,"return LookUpStock()");
"LookUpStock()"
"OnClientClick"
<asp:Button id="btnTest" OnClick="btnTest_OnClick" OnClientClick="LookUpStock()" Text="Test" runat="server" />
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)