Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox not getting all selected items
    text
    copied!<p>I have a ListBox which is populated dynamically from a database in the code behind. I have another button and when i click the button, the button click event will get the all the selected listitem and insert the list item text to the database. I set AutoPostBack as false and EnableViewState is true in the listbox property</p> <p>The problem is when i click the button , it only see 1 selected item even i select multiple items.Here are the codes. I appreciate your help. I spend 1 day on this issue and not getting anywhere.</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { loadrdlist(); } } protected void loadrdlist() { ((ListBox)TestFormView.FindControl("ListBoxB")).Items.Clear(); foreach (FailureTempRD rd in FailureTempRD.SelectFailureTempRD()) ((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator)); } protected void btn_AddRD_Click(object sender, EventArgs e) { foreach (ListItem rd in ((ListBox)TestFormView.FindControl("ListBoxB")).Items) //This is where it only see 1 selected item { if (rd.Selected == true) //put selected item to database } } } </code></pre> <p>Here are both listbox and button</p> <pre><code>&lt;asp:ListBox ID="ListBoxB" runat="server" SelectionMode="Multiple" &gt;&lt;/asp:ListBox&gt; &lt;asp:Button ID="btn_AddRD" runat="server" CausesValidation="False" onclick= "btn_AddRD_Click" Text="Add" /&gt; </code></pre> <p>Update : I figure why. When i load the listitem, i need to add ID as listitem value. So change the following code. I test few times and it works as intend.</p> <pre><code> ((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator)); </code></pre> <p>To</p> <pre><code> ((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ID)); </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