1
votes

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?

1
Please check if in page_load you are binding within ! (IsPostback) clause ? - V4Vendetta
just saw that. facepalm. - randomThought
I guess then that must have fixed your issue - V4Vendetta

1 Answers

2
votes

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

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