0

I want to get a selected item from a drop down list, on button click need to show that selected item into a text box.

I haven't tried anything cause i don't know what to do.

dvdsmpsn
  • 2,839
  • 1
  • 26
  • 40

2 Answers2

1

Just use this code...

Suppose, Button click event:

   protected void Button1_Click(object sender, EventArgs e)
    {
       Textbox1.Text = DropDownList1.SelectedItem.Text.ToString();
    }
Syafiqur__
  • 531
  • 7
  • 15
THE LIFE-TIME LEARNER
  • 1,476
  • 1
  • 8
  • 18
  • thanks this worked, but i didnt need the 'Conver.toString' at the start however when i try add another item from the list it deletes the last item i selected and when they add they are are not at the top of the text box they are in the middle. would you be able to help with this? – Hamish.w100 Jul 15 '19 at 08:11
  • when you try to bind drop down list from database ...make sure that your drop down selected item have order by clause.....if your problem is not solved ...try send your code with proper way...so i can easily find out – THE LIFE-TIME LEARNER Jul 15 '19 at 08:19
  • https://stackoverflow.com/questions/222572/sorting-a-dropdownlist-c-asp-net refet this stackoverflow Link.....for sorting your drop down list – THE LIFE-TIME LEARNER Jul 15 '19 at 08:21
0

The following class can give you the general idea of doing what you want.

public class MyClass
{
    public MyClass() 
    {
        comboBox = new ComboBox();
        button = new Button();
        textBox = new TextBox();
        button.Click += OnButtonClick;
    }

    private void OnButtonClick(Object sender, EventArgs e)
    {
        textBox.Text = comboBox.SelectedItem.ToString();
    }

    ComboBox comboBox;
    Button button;
    TextBox textBox;
}
Razko
  • 551
  • 2
  • 7