Click here to Skip to main content
15,350,252 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an asp ListBox, I have retrieved the items from the database. How can I get the selected item from the Listbox and pass it as a parameter in order to call an action by a Button Click?
Posted

You could do something as simple as

VB
Public Sub Button_Click() Handles Button.Click
Dim Something as String = ListBox.SelectedValue 
'(Or SelectedText - If that is what you want)

SomeFunction(Something)


If you give me a code extract or some more information AKA your buttons name, your list boxes name and the function you are trying to pass information too I will be able to write a better piece of code for you
   
Comments
JOHN_726 10-Aug-12 8:03am
   
This is MVC, this type of button click event won't work
Suppose you have populated ViewBag.ItemsForList = [items you want to appear on the combo box]; i.e. Name of the States in US, and have property called MyItem in your Model. if you do not have the property then you might want to create ViewModel class with the property and use it as Model.

after this when you do following, you will have MyItem populated in your Action as property of Model used when the form is submitted.

C#
@Html.DropDownListFor(model => model.MyItem,
    new SelectList(ViewBag.ItemsForList, "StateID", "StateName"))


you will get ID of the state name selected in your Action.
   

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