Why SelectedIndexChanged fires for a DropDownList when a button is clicked?

Tony_Henrich picture Tony_Henrich · Mar 16, 2010 · Viewed 8.4k times · Source

I have an ASP.NET DropDownList with AutoPostBack=true and EnableViewState=false. I have a button on the page that does nothing. If I change the selection in the ddl, it posts back , which is expected. If I click the button, the page posts back and the ddl's SelectedIndexChanged fires. Why does it get fired?

Answer

Ashish Gupta picture Ashish Gupta · Mar 16, 2010

Feedback from Microsoft :- http://connect.microsoft.com/VisualStudio/feedback/details/103844/dropdownlist-always-fire-selectedindexchanged-event-when-viewstate-is-disabled-and-the-selected-item-is-not-changed-by-the-user

"Thanks for your feedback. If ViewState is disabled on the page or on the DropDownList control, the selected index cannot be saved, so each postback looks like the selected index has been changed. You can save the selected index yourself and compare against it to see if the selection has really changed, or you can enable ViewState on the DropDownList. "

In your case the viewstate of the dropdownlist is false. Enable the same or you can compare index of the selected item as suggested above.