473,796 Members | 2,645 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.le ngth] = 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 5227
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*****@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.le ngth] = 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*****@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.le ngth] = 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*****@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.le ngth] = 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******** ******@TK2MSFTN GP15.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*****@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.le ngth] = 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....Reques t.Form is used to track what was selected....Vie wstate 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*****@hotmai l.com> wrote in message
news:OJ******** ********@TK2MSF TNGP14.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******** ******@TK2MSFTN GP15.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*****@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.le ngth] = 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******** ******@TK2MSFTN GP14.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....Reques t.Form is used to track what was selected....Vie wstate 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
2830
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 debugger it all seems to be working properly until the Load function is called, and then all my selections but 1 are unselected. Anybody had any experience with this and know of a fix? Listbox is databound Listbox appears on a tabpage inside...
17
3134
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) Me.lboxRP.SetSelected(newRPindex, True) When the last line executes, I get an error message:
9
45508
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 the listbox. Thanks, Alpha
6
6509
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 page does not know the items were added client-side.) TIA, Val
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 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 = new Option(name, typeId); This all works fine and I can move across...
0
1690
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 the listbox with no luck and, because it is already bound, I can't just delete the item directly from the listbox without an error (because it already has a datasource, you are not allowed to delete/add anything to the listbox directly). The...
5
2202
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 added a click event handler that resets the selected items based on whats in the collection it is data bound to... I'm baffled, pounding my head against the wall, it's not working... Please help... I tried posting the code but it was too long,...
1
3440
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 foreach (Product p in manager.Products) { ASP.MasterDetailDropDown productMasterDetail; productMasterDetail = (ASP.MasterDetailDropDown)LoadControl("~/controls/MasterDetailDropDown.ascx"); ...
0
2147
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 ControlParameter that has a ControlID that points to "lb_dates". The PropertyName of the ControlParameter is "SelectedValue". The user selects two dates from the ListBox. I am trying to pass BOTH selected values of the ListBox to a SelectMethod of
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.