Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Framework 4 client callback demo code complains about ListBox1
    text
    copied!<p>From this set of ASP.NET demo code:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms178209.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms178209.aspx</a></p> <p>in VS2010 in an ASP.NET project, I get error message: </p> <p>"The name 'ListBox1' does not exist in the current context" from the C# source file where the ListBox1 is referenced (lines 35-37). If I add a declaration for the ListBox1 within the class, the error goes away but when I press F5 to run it, I get a message that the ListBox1 has already been defined. What have I missed?</p> <p>ClientCallback.aspx.cs:</p> <pre><code>using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler { // protected ListBox ListBox1; 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; } } </code></pre> <p>and ClientCallback.aspx:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html &gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;Client Callback Example&lt;/title&gt; &lt;script type="text/ecmascript"&gt; function LookUpStock() { var lb = document.getElementById("&lt;%=ListBox1.ClientID%&gt;"); try { var product = lb.options[lb.selectedIndex].text; CallServer(product, ""); } catch (err) { alert("Please select a product."); } } function ReceiveServerData(rValue) { document.getElementById("&lt;%=ResultsSpan.ClientID%&gt;").innerHTML = rValue; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:ListBox ID="ListBox1" Runat="server"&gt;&lt;/asp:ListBox&gt; &lt;br /&gt; &lt;br /&gt; &lt;input type="button" value="Look Up Stock" onclick="LookUpStock()" /&gt; &lt;br /&gt; &lt;br /&gt; Items in stock: &lt;span id="ResultsSpan" runat="server"&gt;&lt;/span&gt; &lt;br /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload