Philip B
Philip B

Reputation: 27

ASP.NET listbox selected item change textbox text

So I got a listbox with 5 different persons in it. And when I click on the first person in the list a text appears in a textbox. An if I click on another person, we get another text in the textbox etc.

Here is my list:

private List<string> personsList = new List<string>();

personsList.Add("Person1");
personsList.Add("Person2");
personsList.Add("Person3");
personsList.Add("Person4");
personsList.Add("Person5");

ListBoxPersons.DataSource = personsList;
ListBoxPersons.DataBind();

So, if I click of Person1 we get the text "Andersson" in the textbox. If we click on Person2 we get the text "Smith" in the textbox.

I've tried this:

foreach (ListItem item in ListBoxPersons.Items)
{
    if (item.Text == "Person1")
    {
        TextBoxPersons.Text = "Andersson";
    }
    else if (item.Text == "Person2")
    {
        TextBoxPersons.Text = "Smith";
    }
}

And so on, but that didn't work. I have tried quite a few other ways of doing it, but sadly no luck with that either. This may seem silly, but it is just an exercise.

C# or JQuery works for me. Thanks in advance.

Upvotes: 0

Views: 6270

Answers (3)

Reena
Reena

Reputation: 1119

The above code which @sr28 suggested works fine I have tried it. This is the view:

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" 
                    onselectedindexchanged="ListBox1_SelectedIndexChanged">   </asp:ListBox>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>`

on pageload bind the listbox:
           `if (!IsPostBack)
          {
            personsList.Add("Person1");
            personsList.Add("Person2");
            personsList.Add("Person3");
            personsList.Add("Person4");
            personsList.Add("Person5");
            ListBox1.DataSource = personsList;
            ListBox1.DataBind();   
}`

And this on the onSelectedIndexChanged:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
        string curItem = ListBox1.SelectedItem.ToString();
        switch (curItem)
        {
            case "Person1":
                TextBox1.Text = "Andersson";
                break;
            case "Person2":
                TextBox1.Text = "Smith";
                break;
        }
  }

Upvotes: 0

Alan H
Alan H

Reputation: 262

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dictionary<string, string> personsList = new Dictionary<string, string>();

            personsList.Add("Andersson", "Person1");
            personsList.Add("Smith", "Person2");
            personsList.Add("Name 3", "Person3");
            personsList.Add("Name 4", "Person4");
            personsList.Add("Name 5", "Person5");

            ListBoxPersons.DataTextField = "Value";
            ListBoxPersons.DataValueField = "Key";
            ListBoxPersons.DataSource = personsList;
            ListBoxPersons.DataBind();
        }
    }

    protected void ListBoxPersons_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = ListBoxPersons.SelectedValue;
    }

Page

    <asp:ListBox ID="ListBoxPersons" runat="server" OnSelectedIndexChanged="ListBoxPersons_SelectedIndexChanged" AutoPostBack="true"></asp:ListBox>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Upvotes: 0

sr28
sr28

Reputation: 5106

Your current code isn't checking what's currently selected, just each item in the list. You need to create a SelectedIndexChanged event to handle when you select something different. Something like this example on MSDN.

Basically, add the event to your asp.net ListBox control and then create the event in your code behind with the same name something like this:

private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox. 
   string curItem = listBox1.SelectedItem.ToString();

   switch(curItem)
   {
       case "Person1":
           TextBoxPersons.Text = "Andersson";
       break;
       case "Person2":
           TextBoxPersons.Text = "Smith";
       break;
       //Add more cases and perhaps a default...
   }
}

UPDATE

Just saw the comments from @Banana and @PhilipB. As mentioned you need to ensure you wrap the listbox initialization in if(!IsPostback) in your Page_Load event to ensure you don't lose the fact you've selected an item.

Upvotes: 1

Related Questions