0
votes

I'm trying get text of the selected drop down item

My drop down list is filled of db data

ad.Fill(dt);
drop1.DataSource = dt;
drop1.DataTextField = "zodys";
drop1.DataValueField = "zodys";
drop1.DataBind();

for example: word1, word2, word3, ... All this is working fine, but when I try get text of the selected item I always get same text (text of the 1 item)

txtZip.Text = drop1.SelectedItem.Text; 
2

2 Answers

2
votes

I can almost guarantee your problem is that you're defining the above within Page_Load()? You need to only do this if you're not posting back, like so:

if(!IsPostBack)
{
   ad.Fill(dt);
   drop1.DataSource = dt;
   drop1.DataTextField = "zodys";
   drop1.DataValueField = "zodys";
   drop1.DataBind();
}

This ensures that the value is not being reset each time prior to checking the SelectedItem.

1
votes

I assume that you're databinding the dropdown also on postbacks in page_load. You should check for IsPostBack.

if(!IsPostBack)
{
    ad.Fill(dt);
    drop1.DataSource = dt;
    drop1.DataTextField = "zodys";
    drop1.DataValueField = "zodys";
    drop1.DataBind();
}