Note that there are some explanatory texts on larger screens.

plurals
  1. POc#.NET ListBox with dynamic data and multiselection problem
    primarykey
    data
    text
    <p>Im trying to get values from a <code>ListBox</code> that has multiselection and dynamic ListItems in it. My problem is that I can't get the values once I press my "assign" button.</p> <p>The idea is that I have like 10-15 listitems and they should be assigned with values. And I want to be able to select like 5 of them and press my assign button and then continue to assign other values to these items left in the list.</p> <p>For som reason my <code>ListBox.Item.Count</code> always returns like 1 row.</p> <p><em><strong>Question: Is there something you need to think about when using <code>ListBox</code> in .NET 4.0 that isn't obvious?</em></strong></p> <p>Parts of the code</p> <p>Front:</p> <pre><code> &lt;fieldset&gt; &lt;legend&gt; &lt;asp:Label ID="Label1" runat="server" Text="&lt;%$ Resources:lang, ExtensionsTitleAB %&gt;"&gt;&lt;/asp:Label&gt; &lt;/legend&gt; &lt;div class="extLeft"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID="Label5" runat="server" Text="&lt;%$ Resources:lang, ExtensionsTmNumber %&gt;"&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat="server" ID="txtTmNumber"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID="Label6" runat="server" Text="&lt;%$ Resources:lang, ExtensionsTmName %&gt;"&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat="server" ID="txtTmName"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID="Label7" runat="server" Text="&lt;%$ Resources:lang, ExtensionsTmRegistered %&gt;"&gt; &lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat="server" ID="txtTmRegistered"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID="Label8" runat="server" Text="&lt;%$ Resources:lang, ExtensionsTmLocality %&gt;"&gt; &lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat="server" ID="txtTmLocality"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID="Label9" runat="server" Text="&lt;%$ Resources:lang, ExtensionsMemberShipId %&gt;"&gt; &lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat="server" ID="txtMembershipIdFaseOne"&gt; &lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div class="extMiddle"&gt; &lt;asp:Button runat="server" ID="btnAssignAB" OnClick="btnAssign_Click" Text="&lt;%$ Resources:lang, ButtonAssignExtensions %&gt;" /&gt; &lt;/div&gt; &lt;div class="extRight"&gt; &lt;p&gt;&lt;asp:Label ID="Label4" runat="server" Text="&lt;%$ Resources:lang, ExtensionsListHelp %&gt;"&gt; &lt;/asp:Label&gt; &lt;/p&gt; &lt;asp:ListBox runat="server" ID="listABContainer" SelectionMode="Multiple" AutoPostBack="false"&gt; &lt;/asp:ListBox&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>The codebehind:</p> <pre><code>public partial class extensions : System.Web.UI.Page { private model.OrderHandling orderHandling; protected void Page_Load(object sender, EventArgs e) { if (Session["dataobject"] != null) { orderHandling = (OrderHandling)Session["dataobject"]; } else { String url = "http://" + Request.Url.Authority + "/Default.aspx"; Response.Redirect(url); } if (!IsPostBack) { addListItems(); } } private void addListItems() { foreach (OrderLine line in orderHandling.order.getOrderLines()) { if (line.price.getPriceType().Equals(Price_Types.SUNRISE_ONE) || line.price.getPriceType().Equals(Price_Types.SUNRISE_TWO)) { listABContainer.Items.Add( new ListItem(line.domain.domainName, line.domain.domainName)); System.Diagnostics.Debug.WriteLine("added a domain " + "to list listABContainer : " + line.domain.domainName); } else if (line.price.getPriceType().Equals(Price_Types.LANDRUSH) || line.price.getPriceType().Equals(Price_Types.GENERAL)) { listCDContainer.Items.Add( new ListItem(line.domain.domainName, line.domain.domainName)); System.Diagnostics.Debug.WriteLine("added a domain " + "to list listCDContainer : " + line.domain.domainName); } } } protected void btnAssign_Click(object sender, EventArgs e) { assignTMExtensions(); } private bool assignTMExtensions() { bool success = true; TradeMarkExtension tmExt = new TradeMarkExtension(); String errorMsg = ""; if (!String.IsNullOrEmpty(txtTmNumber.Text) &amp;&amp; tmExt.isValid(EXTENSION_KEYS.TM_NUMBER, txtTmNumber.Text, null)) { tmExt.setExtension(EXTENSION_KEYS.TM_NUMBER, txtTmNumber.Text); } if (success) { System.Diagnostics.Debug.WriteLine("Succes: time for domainExtension, listCount : " + listABContainer.Items.Count); foreach (ListItem list in listABContainer.Items) { if (list.Selected) { System.Diagnostics.Debug.WriteLine("Found a selected item " + list.Value); try { OrderLine ol = orderHandling.order.getOrderLine(list.Value); ol.domain.addExtension(tmExt); System.Diagnostics.Debug.WriteLine("Addedd domainExtension"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("FAILED domainExtension"); showError.Text = "An exception has occured. Please reload the page and try again."; return false; } } } Session["dataobject"] = orderHandling; removeListItems(listABContainer); } else { showError.Text = errorMsg; } return success; } private void removeListItems(ListBox list) { int i = 0; while(i &lt; list.Items.Count) { if (list.Items[i].Selected) { list.Items.RemoveAt(i); } i++; } } } </code></pre>
    singulars
    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