473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5163
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.