Every time I click an ASP button on my page to select an item in my dropdown list, it is only choosing the first item in the list. It is as if every time I click a button the page refreshes and all variables reset. How do you choose an item from a dropdown when using dynamic data from a database?
Share
This sounds indicative of populating the
DropDownListinPage_Load. Is that the case? (I’m assuming you’re using WebForms.) If you’re populating the control inPage_Loadthen you’ll want to wrap it in a conditional:Otherwise, they’ll get re-populated with each postback. When you click a button or perform some other action on the page which initiates a postback,
Page_Loadis called before the event handler. So in effect, this is happening:Page_Loadclears and re-populates theDropDownListDropDownListPage_Loadclears and re-populates theDropDownListDropDownList, which is the default.