Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownList doesn't postback on SelectedIndexChanged

I'm writing an ASP.Net webform with some DropDownList controls on it. Then user changes selected item in one of dropdowns, ASP.Net doesn't seem to handle SelectedIndexChanged event until form is submitted with a 'Submit' button click. How do I make my dropdowns handle SelectedIndexChanged instantly?

P.S. It's a classic question I have answered too many times, but it seems no one asked it before on stackoverflow.

like image 996
Sergey Volegov Avatar asked Sep 16 '08 07:09

Sergey Volegov


4 Answers

Setting the AutoPostback property to true will cause it to postback when the selection is changed. Please note that this requires javascript to be enabled.

like image 161
Richard Szalay Avatar answered Oct 19 '22 13:10

Richard Szalay


You need to set the AutoPostBack property of the list to true.

Also, if you're populating the contents of the drop down list from the code behind (getting the contents of the list from a database, for example) - make sure you're not re-binding the data in every postback.

Sometimes people are caught out by binding the drop-down in the page load event without putting it in an If Not IsPostBack. This will cause the event not to fire.

The same is also true of repeaters and ItemCommand events.

like image 29
Chris Roberts Avatar answered Oct 19 '22 14:10

Chris Roberts


if you are populating the dropdown list during page load then each time the page postback it will reload the list thus negating your postback method. you need to be sure to load the dropdownlist only if (!ispostback)

like image 4
Dave Avatar answered Oct 19 '22 13:10

Dave


Set the AutoPostBack property of DropDownList to true.

like image 3
Haacked Avatar answered Oct 19 '22 15:10

Haacked