In ASP.NET when a post back to the server occurs and it
happens that a control of type ListControl is present on the ASPX page, the ListControl
functions as follows.
Items present in the ListControl will not be posted back to
the server whether in the Forms collection or the QueryString collection
depending on the Action method you are using in the Web Form.
Only the selected value is sent back to the server upon a
post back. This explains how a selected item can be detected on the server.
When the response is back to the client, the ListControl
items are retrieved from the ViewState.
The above facts pop up a serious problem, especially when
you are doing any changes on the client side. Changes can be removing or
adding new items to the ListControl.
Any changes on the client side will not be reflected, since
the shown items on the client side of a ListControl are not posted back with
the page post back. Whatever changes you do, they will disappear once a response
is sent back from the server to the client (browser), since the ListControl as
mentioned above retrieves its values from the ViewState.
In some applications there is a need to add new items to a
ListControl or even remove some items. Imagine two ListControls placed side by
side with some buttons in between to move items between those two ListControl. For
instance, you might have two ListBoxes to hold finished and unfinished tasks,
when a task is finished you move it to the other ListBox and if a task has been
moved, however, you discover later on that some fixes are needed, so you bring
it back to the unfinished ListBox.
To have good performance for you web form, such movements
the server on every movement. However, as listed above, there is a problem that
those changes will not be preserved by the ListBox, because of the default
behavior of the ListBox or any other ListControl in ASP.NET.
This article is meant to solve this problem by making the
ASP.NET ListBox in particular a smart ListControl, where it preserves its
changes upon posting back to the server. Changes can be done by either adding
new items or removing items from its collection of items.
We have taken as an example the ListBox. However, the same
technique can be used to make any control in ASP.NET that implements
ListControl a smart control.