SelectedIndexChanged for programmatically created dropdownlist in ASP.NET fires multiple times.

Posted by Achilles on Stack Overflow See other posts from Stack Overflow or by Achilles
Published on 2010-03-12T19:13:27Z Indexed on 2010/03/12 19:17 UTC
Read the original article Hit count: 632

Consider the following:

dim dropdownlist1 as new dropdownlist
dim dropdownlist2 as new dropdownlist
dim dropdownlist3 as new dropdownlist

dropdownlist1.AutoPostBack = true
dropdownlist2.AutoPostBack = true
dropdownlist3.AutoPostBack = true

AddHandler dropdownlist1.SelectedIndexChanged, AddressOf SomeEvent
AddHandler dropdownlist2.SelectedIndexChanged, AddressOf SomeEvent
AddHandler dropdownlist3.SelectedIndexChanged, AddressOf SomeEvent

The SomeEvent fires as expected when any of the dropdown's selection is changed. However if say DropdownList2 has a selection made then I make a selection with either DropDownList1 or DropdownList3, then SomeEvent fires again. What is causing this behavior and how do I get just a single raising of that event?

I suspect that when the viewstate for the dynamcially created dropdownlists is restored and the selection restored, then the event is fired because technically the selected index did change when the control was recreated. The reason I suspect this is that the event fires the for each dropdownlist...

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about events