Problem with Dropdown - ViewState and AutoPostBack

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

I have a page that works fine. I am trying to optimize the page by
overriding some of the Information that is being saved in the hidden
VIEWSTATE.

If I make the properties of the dropdown False for ViewState and True for
AutoPostBack, it does reload the page but does not call the
SelectedIndexChanged event. If I put the ViewState to True then it is
called.

Does this mean that I cannot get that event and deal with it in better code
than what is required by using both the ViewState and AutoPostBack to true.
I want to cache some info so that is retrieved only when necessary not on
every page load.
 
More info. A small project with a button and a listbox (ViewState =
"False") causes the following. If I click the listbox to change the
selection nothing happens. If I then click the button first the
ListBoxSelection Change happens then the Button Click.

Does this mean that the only way to get the listbox to send events (when
they happen) is to have the view state enabled??

If so I can only see this as a problem if you are trying to optimize the
code for page loads.
 
By default, the autopostback property of non-button type controls is set to
false. Without a post back your selection change event will not fire until
the next post back. When a form is posted back, all pending events are
processed. This is why the selection change happens as well as the button
click when you click it. To make the page process the selection change box
immediately, you need to set the autopostback property of the listbox to
true. This is completely unrelated to the viewstate.



I was minding my own business when Lloyd Sheen blurted out:
 
With the listbox, the items are saved in the viewstate. So the first time
your page is rendered you add the items to the listbox and everything is
fine. When a postback happens Load View State takes place, which repopulates
the listbox with the items that the listbox originally contained. Then after
this Load Postback Data happens which applies the user's selection to the
list box. If a selection took place, then the event will be fired latter on
in the page processing.

When you don't have viewstate enabled, on Load View State nothing in the
ListBox is repopulated, and when Load Postback Data happens the user
selection comes in but there are no items in the Listbox to apply the user
selection to, hence no event. If you don't want to use viewstate, you have
to repopulate the listbox yourself before Load Postback Data happens.
Essentailly you are taking over the view states responsibility but the data
is coming from server side cache, database, file, etc...
 
Back
Top