views:

2000

answers:

4

I've found many posts where people try to work around the problem of SelectedIndexChanged not working when EnableViewState='false'.

Im a little confused why control state doesnt kick in and allow it to work, but thats bonus points if anyone can explain that too.

Some of the 'hacks' are pretty 'hacky'. Like setting a value on the viewstate of the page itself corresponding to the dropdown value and then comparing that value with the one that is received durin the postback.

I'm lookin for the best most elegant solution (if there is a good one).

I dont know if this would be a clever derived control or something cleverer, but I want to make sure the solution is generic enough to be 'trustable' and not just a hack that only works for the specific page in question (which is common for such hacks!).

A: 

Try loading your data into the DropDownList at an earlier point. Ie. in InitComplete. I haven't tested it, but I think that should do the trick.

EDIT:

It didn't do the trick. I've tested it, and it doesn't work satisfactory. As Ken Browning noted in another answer on this question, the SelectedIndexChanged event is ALWAYS fired if the SelectedIndex is any other than its initial value.

Why isn't SelectedIndex stored in ControlState instead of in ViewState. Can we trick ViewState to only keep track of SelectedIndex, while not storing the Items collection?

Arjan Einbu
as explained in the 'truly' understanding viewstate article below - if you bind the control AFTER you add it to the control array then it wont save the data in viewstate.
Simon_Weaver
+4  A: 

If you really want to know why and how this works, I'd suggest the following article:

TRULY Understanding ViewState

It's not an easy read, but does cover all the problems that people commonly encounter with viewstate. And it covers why everything works if you populate in Page_Init as opposed to Page_Load.

feihtthief
i actually did read this article before i posted this. was very interesting but left a few unanswered questions. i posed this question acting slightly 'dumb' because i didnt want to overcomplicate it by trying to refer to that article.
Simon_Weaver
A: 

Bind the list before adding it to the control collection (not after as above commenter says).

-author of said article :)

InfinitiesLoop
A: 

dropdown selectedindexchanged then its related textbox will binding from database in asp.net give example plz

keyur