473,511 Members | 15,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listbox loses values

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

Nov 19 '05 #1
6 5180
This is not a known problem, or even a problem at all, well, for you it
seems to be. :)

The server sends the second listbox to the browser with no items in the
collection, and places zero items into ViewState.

Client side you add options to the second list box.

The page posts back with only the value from the second list box.

The second list box control loads its items collection from ViewState
(remember there are zero items).

So there are no items in the second list box. This is the way server side
controls work.

Two ways around it. Either you can post back with each movement of the
item, or post a list of the values from the first list box and on postback
manually move a copy of the items for the lbFirst.Items collection into the
lbSecond.Items collection.

HTH,

bill

"Janaka" <ja*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 19 '05 #2
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" <ja*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 19 '05 #3
This is not a known problem, or even a problem at all, well, for you it
seems to be. :)

The server sends the second listbox to the browser with no items in the
collection, and places zero items into ViewState.

Client side you add options to the second list box.

The page posts back with only the value from the second list box.

The second list box control loads its items collection from ViewState
(remember there are zero items).

So there are no items in the second list box. This is the way server side
controls work.

Two ways around it. Either you can post back with each movement of the
item, or post a list of the values from the first list box and on postback
manually move a copy of the items for the lbFirst.Items collection into the
lbSecond.Items collection.

HTH,

bill

"Janaka" <ja*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 19 '05 #4
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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:e9**************@TK2MSFTNGP15.phx.gbl...
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" <ja*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 19 '05 #5
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" <ja*****@hotmail.com> wrote in message
news:OJ****************@TK2MSFTNGP14.phx.gbl...
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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:e9**************@TK2MSFTNGP15.phx.gbl...
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" <ja*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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




Nov 19 '05 #6
Hi Karl,

Thanks for your response that clears things up, I wish someone could of
explained that to me rather than saying "its the viewstate".

I managed a workaround to get this running. I placed the values comma
delimited into an HTMLInputHidden control and then binding the data back to
the ListBox in Page_Load and the Request.Form took care of what was
selected.

After thinking about it, would it make sense to override the SaveViewstate()
event on the Page and implement some custom code to add the new items to the
ListBox? I haven't tried monkeying about with Viewstate in the past but it
seems to make sense.

Janaka

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uy**************@TK2MSFTNGP14.phx.gbl...
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/


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2793
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in information in the database. When I step through the...
17
3089
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
9
45436
by: Alpha | last post by:
Hi, How can I set all the items in a listbox to be selected? I can't find a property or mehtod to do it so I thought I'll try using setselected method but I need to find out how many items are in...
6
6493
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
1
573
by: Janaka | last post by:
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...
0
1675
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
5
2184
by: Alien2_51 | last post by:
I have a problem with a ListBox control that is on a TabControl, it seems to be forgetting which items are selected in the list when I tab off the current tab, here's my winform code... I even...
1
3393
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
0
2118
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a ListBox server control named "lb_dates" with a SelectionMode of "Multiple". The user can select multiple dates from the listbox. I have ObjectDataSource named "ods_rfq" with a...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.