Textboxes don't need viewstate to retain their state....that's because they
are single valued controls. When you put text in textbox and submit the
form, the value that was entered in the textbox is available via
Request.Form. When you put a bunch of values in a listbox, and submit the
form, the SELECTED values are available via Request.Form but the other
values are not...in either case, the text of the listbox (Even those
selected) isn't persisted in REquest.Form...
The way it works is
(a) the page is rendered to the browser
(b) Every listitem object is serialized to the viewstate (namely, their
value and text are stored there)
(c) items are selected
(d) the form is submitted
at this point the page framework:
(e) restores all ListItems frm the viewstate
(f) uses Request.Form to see which items were selected
(g) selects the appropriate ListItems
as you can see, Request.Form and viewstate are playing off of each
other....Request.Form is used to track what was selected....Viewstate to
track everything (except you don't know which was selected). The value
entered in a textbox IS the selected value...so all you need is Request.Form
to preserve the state...which, in your example you have...
Hope that cleared it up.
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
Janaka said:
That doesn't seem accurate to me Karl. I placed a TextBox on the form as
well and when I used the javascript code to copy items I also got it to
update the text value of the control. This DID retain its value on postback
and was done through client-side script.
Karl Seguin said:
Janaka:
The reason listboxes (and other controls) maitain their values is because
these values are saved in the viewstate when the page is first rendered to
the client. Since you are populating your 2nd listbox using javascript,
these values are never stored in the viewstate, meaning the listbox can't
automatically be repopulated on postback. You can use the good'ol
Request.Form to get the selected typeId's of the 2nd listbox, or instead
of
populating the 2nd listbox in the client, you can do so on the server (by
posting back and doing what you do in javascript in server-code)...but
those
are your only choices.
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
Janaka said:
Help! I have two ListBox controls on my web form. The first one gets
populated on entry with values from the DB. I then use JavaScript to
copy
options from this ListBox to my second one. (I have also tried
changing
the
second ListBox to an HtmlSelect control) using bog standard JavaScript code
like so where "used" is the name of my <select> control:
used.options[used.options.length] = new Option(name, typeId);
This all works fine and I can move across the values and names of my
items
in the first select box to the second one. The problem is that when I
postback the entire contents of the second select box are lost! Is this
a
known problem using ListBoxes? Is there a way around it?
Regards,
Janaka