- Home
- Forum
- ASP.NET Web Forms
- Get selected entity from list box
Get selected entity from list box
I fill a list box with SelectMethod, and when the user click on process, I need the selected Item from ListBox, but I don't know how get this class:
The Items collections are empty, but the items are showed, selectedIndices always return 0, only SelectedItemIndex works, but I don't want the index, I want the entity or the value field.
I've a similar problem with the dropdownlist
Also, the visualization is wrong
I attach a similar project for test
Attachment: WebApplication1_42b5d53a.zip
Hi Manolo,
Sorry for the inconvenient.
Since the reported requirement as a defect, we have created an incident in your account. Kindly follow up the incident for further queries.
Please let me know if any concerns.
Regards,
Kalai Selvi
Use the following code to resolve this issue:-
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { // Get the currently selected item in the ListBox. string curItem = listBox1.SelectedItem.ToString(); // Find the string in ListBox2. int index = listBox2.FindString(curItem); // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2. if(index == -1) MessageBox.Show("Item is not available in ListBox2"); else listBox2.SetSelected(index,true); }
Helpful resource :-https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem%28v=vs.110%29.aspx
Thanks!!
This is the problem.
And.... what about the visualization? Why when I select the second or third items, the text are moved?
We have analyzed your requirement “to get the selectedItems in the listbox” can be achieved by using following code snippet to your application.
|
protected void butn_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e) { //to assign the selected items in the listbox controls int selectItemIndex = ListBox1.SelectedItemIndex; Item selectedItem;; if (selectItemIndex == -1) selectedItem = null; else selectedItem = GetItems().ElementAt(selectItemIndex); // to get the element based on our selected index //get the text from the selected element if its not means return the message var selectedText = selectedItem!=null? selectedItem.Name:"Item is not available in ListBox2"; |
And also we have prepared a simple sample to exhibit your requirement, please download from the below downloadable link.
Sample Location: Sample
If still you have face any difficulties while get the element, please revert back us we will happy to assist you.
Regards,
Kalpana K
Thanks for your answer. It's a temporal solution.
But I've other question.... in my example, when I select the second or third item, my visualization is wrong, and in your example it works fine, Why?
After analyzing more about your sample, we have found that the reported issue “The visualization problem after selecting the second or third item” caused due to the bootstrap CSS used in your application.
In our last update, we have provided sample does not include the bootstrap CSS file.
We have confirmed this as a defect at our end and logged defect report regarding this. A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates
https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents
Please let us know if you need any further assistance.
Regards,
Kalpana K
- 8 Replies
- 4 Participants
-
MA Manolo
- Dec 11, 2015 04:58 PM UTC
- Dec 18, 2015 12:37 PM UTC