Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX with ASP.NET 4 - Using ajax to add to a ListItem
    primarykey
    data
    text
    <p>Firstly I'd like to point out that I am very new to working with the MS Ajax framework within ASP.NET so there might be something incredibly simple and fundamental that I am doing wrong... I am more than happy to accept that!</p> <p>Anyway, I am trying to add items to a listbox by sending a new serial number up to the server, validating it and then updating the listbox control from the server side. The first time I click on the button I get the desired result - the new serial number is sent to the server, added to the server's instance of the listbox class and then sent back to the client. So far so good...</p> <p>Unfortunately, the next time I click on the add button I get an exception:</p> <blockquote> <p>Unhandled exception at line 881, column 13 in <a href="http://ajax.aspnetcdn.com/ajax/4.5/3/MicrosoftAjaxWebForms.debug.js" rel="nofollow">http://ajax.aspnetcdn.com/ajax/4.5/3/MicrosoftAjaxWebForms.debug.js</a></p> <p>0x800a139e - Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An item with the same key has already been added.</p> </blockquote> <p>This second command doesn't appear to be making it up to the server, the exception seems to be on the client side before it reaches the server (I verified this by placing a breakpoint and trace commands on the server on the button click event handler).</p> <p>I'm not sure what key it is referring to and I'm not familiar enough with the ajax framework to try and trace the problem through the nest of javascript!</p> <p>I know that the problem isn't anything to do with my usage of the listbox as the problem is still present when I only have the button and text box.</p> <p>I have included my ASP code and the code-behind or a very simple application that I can reproduce this problem on. If anyone can shed any light on this exception I would greatly appreciate it - any searching that I've done so far has only come up with people using databinding and there genuinely being duplicate keys... I don't see where I am even using a key! I'm sure it is something being registered internally in the ajax framework.</p> <pre class="lang-html prettyprint-override"><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest.aspx.cs" Inherits="AjaxTest" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:ScriptManager runat="server" EnableCdn="true" ID="ScriptManager" /&gt; &lt;asp:UpdatePanel ID="ListBoxPanel" runat="server"&gt; &lt;ContentTemplate&gt; &lt;asp:ListBox ID="lstSerialNumbers" runat="server" /&gt;&lt;br /&gt; &lt;asp:TextBox ID="txtNewSn" runat="server" MaxLength="50" /&gt;&lt;br /&gt; &lt;asp:Button ID="btnAddSn" runat="server" OnClick="btnAddSn_Click" Text="Add"/&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <pre class="lang-cs prettyprint-override"><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Test { public partial class AjaxTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAddSn_Click(object sender, EventArgs e) { lstSerialNumbers.Items.Add(new ListItem(txtNewSn.Text)); txtNewSn.Text = ""; } } } </code></pre> <p>Let me know if there is any additional information that I can provide.</p> <p>Regards, Anthony</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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