Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to retain listbox items on multiple clicks on list box...on multiple clicks there will be multiple values will get know...but i am getting only last click selected value...but i need all selected item values of multiple clicks....

thanking you

What I have tried:

for (int i = 0; i < lboxavilableInsName.Items.Count; i++)
               {
                   if (lboxavilableInsName.Items[i].Selected)
                   {
                       if (!arraylist1.Contains(lboxavilableInsName.Items[i]))
                       {
                           arraylist1.Add(lboxavilableInsName.Items[i]);
                           arrUpdatedInsValues.Add(lboxavilableInsName.Items[i].Value);
                           arrUpdatedInsNames.Add(lboxavilableInsName.Items[i].Text);
                       }
                       ViewState["UpdatedInsValues"] = arrUpdatedInsValues;
                       arrUpdatedInsValuestotal = (ArrayList)ViewState["UpdatedInsValues"];
                       ViewState["UpdatedInsValues2"] = arrUpdatedInsValuestotal;
                       ViewState["UpdatedInsNames"] = arrUpdatedInsNames;
                   }
Posted
Updated 21-Jan-17 1:54am
v2

1 solution

You need to update the SelectionMode property to Multiple to ensure that the users can select multiple values from the list. The default value for this is Single and that is what you are seeing as you only get the last one as selected.
<asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
     <asp:ListItem Value=".89">apples</asp:ListItem>
     <asp:ListItem Value=".49">bananas</asp:ListItem>
</asp:ListBox>

The code was taken from the following MSDN pages, you can try it out in your own code. If I understand correctly you might require to hold CTRL key to select multiple values; or maybe ASP.NET Web Forms stores that as well.

ListBox.SelectionMode Property (System.Web.UI.WebControls)[^]
ListSelectionMode Enumeration (System.Web.UI.WebControls)[^]
 
Share this answer
 
Comments
BillWoodruff 22-Jan-17 20:40pm    
+5
Afzaal Ahmad Zeeshan 23-Jan-17 5:24am    
Thank you, Bill.
sreeenivasa reddy 23-Jan-17 0:32am    
Hi thanks for your reply...but this is not exactly what i want...actually i have given selsectionmode="Multiple" in the listbox...that will select multiple items when we select first time or subsequesnt time after page gets loaded..but i want that in a code behind that saying ex: if i select 2 items 1st time that items will added in the second listbox and i will get the values of those selected items....if again i select any items after adding previous selection items in 2nd listbox...i want the item value..selected at 2nd time along with first two item values..so totally 3 values i want...and i need to send that values to the stored procedure to insert.....
Afzaal Ahmad Zeeshan 23-Jan-17 5:27am    
For that you either need to store the selected values to Session, or you need to store the selections in the database and then fetch them from database later.

Since you are storing the data to storage medium; database. What code are you using? I cannot provide you any code or help unless given some more information; such as database table schema, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900