Click here to Skip to main content
15,883,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have a Listbox in my project like this:
C#
<asp:ListBox runat="server" ID="MultiSelect" SelectionMode="multiple">
                               
                            </asp:ListBox>



C#
<p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" ></asp:TextBox>


Code behind:

C#
//on page load i fill the the Listbox with values like this:

MultiSelect.Items.Add(new ListItem {Text = "John Doe", Value = "1"});

MultiSelect.Items.Add(new ListItem {Text = "Jane Doe", Value = "2"});
// 
listBoxValue.Text = MultiSelect.SelectedValue // << i know this is wrong but this is where im stuck


I need to get value of the select item of the listbox and display it. So for example when a user selects "John Doe" from the listbox , I want the value to be filled in the textbox but i'm not sure how to this.

PS I don't want to do it with a button click , i would like it to be done when the user selects from the Listbox
Thank you

What I have tried:

Well, I have been looking at event listeners but i'm not sure which one might be the best to accomplish this, at the moment i'm only comfortable with button click but in this case i dont need the button click event listener
Posted
Updated 19-Nov-17 23:16pm
Comments
Sinisa Hajnal 20-Nov-17 5:12am    
There has to be an event SelectedItemChanged or SelectedIndexChanged or some such - just read through the documentation.

1 solution

Markip code

<form id="form1" runat="server">
    <div>
    <asp:ListBox runat="server" ID="MultiSelect" AutoPostBack="true" SelectionMode="multiple" OnSelectedIndexChanged="MultiSelect_SelectedIndexChanged">
                               
                            
    </div>
        <div>
            <p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" >
        </div>
    </form>




Code behind
public partial class list_show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiSelect.Items.Add(new ListItem { Text = "John Doe", Value = "1" });

                MultiSelect.Items.Add(new ListItem { Text = "Jane Doe", Value = "2" });
                
            } 
        }

        protected void MultiSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxValue.Text = MultiSelect.SelectedItem.Text;
        }
    }
 
Share this answer
 
v2
Comments
1Future 20-Nov-17 6:51am    
Thank you

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