randomThought
randomThought

Reputation: 6393

Dropdown list selecteditem or selectedindex not working as expected

I have a DropDownList and a function that gets what the value is selected but the SelectedIndex and the SelectedValue always return the first item.

The DropDown code is

<asp:DropDownList ID="lstApps" runat="server" DataSourceID="sqlDataSource" 
                        DataTextField="some_val" DataValueField="some_id"
                        TabIndex="5" >
</asp:DropDownList>

and the code (in a button click even of a button somewhere on the page)

int x = lstApps.SelectedIndex;

always returns 0 despite of what I might have selected. Is it due to auto postback being disabled or some other reason?

Upvotes: 1

Views: 706

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

I guess! You need to use IsPostBack block in Page_Load event.

public void Page_Load()
{
  if(!IsPostBack) 
  {
     //put databinding code here.
  }
}

Upvotes: 2

Related Questions