Why does SelectedIndexChanged fire for a DropDownList when a button is clicked?

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 ddl, it will go back, as expected. If I click the button, the page will return and ddl SelectedIndexChanged will start working. Why does this work?

+7
drop-down-menu viewstate autopostback
source share
4 answers

Microsoft Feedback: - 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

"Thank you for your feedback. The ViewState is disabled on the page or in 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 it to see if the selection has really changed , or you can enable ViewState on the Drop-down list. "

In your case, the viewstate from the dropdown is false. Include the same, or you can compare the index of the selected item, as suggested above.

+10
source share

Try to enable viewstate. This is a common problem.

EDIT

If you do not want to include the viewstate, you will need to track the value of the jump list yourself, for example, this guy made DropDownList OnSelectedIndexChange for the 0th index without ViewState

+2
source share

If you load your ddl into page_load, when you click the button, it starts page_load again and again loads the ddl elements that change the selected index. But I do not know your code, so this is an assumption.

+1
source share

I had the same problem. I found that the problem is that I called the Render Page_Load function.

 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender 

I completely missed the fact that the page loaded Page_Load as the default loading function, and then loaded it again when prerender started. I changed it to the following, and now the function is called only once:

 Sub Renderer(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender 
0
source share

All Articles