473,396 Members | 2,018 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 data going away

I have a .net app that a user currently enters a number in a text box,
hits a button and a data call is executed. She wants the ability to
enter in multiple numbers (up to 100).

So to make things look better visually for that, I created a listbox
under the text box. She enters the number in the text box, clicks
another button I added and the number is stored in the list box. Then
my plan was to grab all those numbers from the list box when she
clicks the original button to do the data call.

Here's my problem. I filled the listbox using java script - to
prevent hitting the server every time the user clicks my "add"
button. The listbox however is a server side lisbox. But, the moment
I hit my original button (i.e."Run Report"), the data goes away in the
listbox. I have tried to use the EnableViewState property and that
doesn't work. I've tried using the AutoPostBack property and that
doesn't work.

I was told the issue is that the only thing retained in the list box
is the item selected. So I tried to write a javascript method that
would select all of them when the user clicks the "Run Report"
button. It doesn't work, because I have to register the javascript
code in my code behind and it clears that listbox before it executes
even one line of code in the codebehind.

I tried to create a hidden text box to just store the numbers too and
that didn't work. I can't use a <inputwith a type of hidden cause
that is a client control and I need this data on the server side. I
tried to just create a asp text box and make it's visible property
false, but then I can't access the text box on the client side when I
want to fill it.

I think I'm resigned to using a multi-line textbox to just let the
user enter them in all at once, but I hate the idea because it leads
the user down a path of potentially making many mistakes as he/she
enters. I just can't believe that their is no way to keep this data.
What am I missing?
Nov 7 '08 #1
15 2330
"Doogie" <dn******@dtgnet.comwrote in message
news:10**********************************@k1g2000p rb.googlegroups.com...
>I have a .NET app that a user currently enters a number in a text box,
hits a button and a data call is executed. She wants the ability to
enter in multiple numbers (up to 100).
OK.
So to make things look better visually for that, I created a listbox
under the text box. She enters the number in the text box, clicks
another button I added and the number is stored in the list box. Then
my plan was to grab all those numbers from the list box when she
clicks the original button to do the data call.
Sounds reasonable.
Here's my problem. I filled the listbox using JavaScript - to
prevent hitting the server every time the user clicks my "add"
button. The listbox however is a server side lisbox. But, the moment
I hit my original button (i.e."Run Report"), the data goes away in the
listbox. I have tried to use the EnableViewState property and that
doesn't work. I've tried using the AutoPostBack property and that
doesn't work.
That's standard behaviour for ListBoxes - changes made to their elements
collection via client-side JavaScript do not survive a postback.
I was told the issue is that the only thing retained in the list box
is the item selected. So I tried to write a JavaScript method that
would select all of them when the user clicks the "Run Report"
button. It doesn't work, because I have to register the JavaScript
code in my code behind and it clears that listbox before it executes
even one line of code in the codebehind.
If your JavaScript is supposed to select all of the elements in a ListBox
but, in fact, clears the ListBox, then there is either a bug in your
JavaScript, or it's not running at the correct place in the page cycle. This
would need to run in the OnClientClick event of the <asp:Button /control
which does the postback.
I tried to create a hidden text box to just store the numbers too and
that didn't work.
Can you be more specific?
I can't use a <inputwith a type of hidden cause that is a client control
and I need this data on the server side.
Yes you can - just give it a runat="server" tag...
I tried to just create a asp text box and make its visible property
false, but then I can't access the text box on the client side when I
want to fill it.
That's because setting a webcontrol's Visible property to "true" prevents
that control from being rendered to the client browser.
I think I'm resigned to using a multi-line textbox to just let the
user enter them in all at once, but I hate the idea because it leads
the user down a path of potentially making many mistakes as he/she
enters. I just can't believe that their is no way to keep this data.
What am I missing?
A hidden textbox is definitely the way to go here, so I'd suggest that your
JavaScript is where the problem lies.

Please show your JavaScript and how you call it.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #2
On Nov 7, 6:55*am, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"Doogie" <dnlwh...@dtgnet.comwrote in message

news:10**********************************@k1g2000p rb.googlegroups.com...
I have a .NET app that a user currently enters a number in a text box,
hits a button and a data call is executed. *She wants the ability to
enter in multiple numbers (up to 100).

OK.
So to make things look better visually for that, I created a listbox
under the text box. *She enters the number in the text box, clicks
another button I added and the number is stored in the list box. *Then
my plan was to grab all those numbers from the list box when she
clicks the original button to do the data call.

Sounds reasonable.
Here's my problem. *I filled the listbox using JavaScript - to
prevent hitting the server every time the user clicks my "add"
button. *The listbox however is a server side lisbox. *But, the moment
I hit my original button (i.e."Run Report"), the data goes away in the
listbox. *I have tried to use the EnableViewState property and that
doesn't work. *I've tried using the AutoPostBack property and that
doesn't work.

That's standard behaviour for ListBoxes - changes made to their elements
collection via client-side JavaScript do not survive a postback.
I was told the issue is that the only thing retained in the list box
is the item selected. *So I tried to write a JavaScript method that
would select all of them when the user clicks the "Run Report"
button. *It doesn't work, because I have to register the JavaScript
code in my code behind and it clears that listbox before it executes
even one line of code in the codebehind.

If your JavaScript is supposed to select all of the elements in a ListBox
but, in fact, clears the ListBox, then there is either a bug in your
JavaScript, or it's not running at the correct place in the page cycle. This
would need to run in the OnClientClick event of the <asp:Button /control
which does the postback.
I tried to create a hidden text box to just store the numbers too and
that didn't work.

Can you be more specific?
I can't use a <inputwith a type of hidden cause that is a client control
and I need this data on the server side.

Yes you can - just give it a runat="server" tag...
I tried to just create a asp text box and make its visible property
false, but then I can't access the text box on the client side when I
want to fill it.

That's because setting a webcontrol's Visible property to "true" prevents
that control from being rendered to the client browser.
I think I'm resigned to using a multi-line textbox to just let the
user enter them in all at once, but I hate the idea because it leads
the user down a path of potentially making many mistakes as he/she
enters. *I just can't believe that their is no way to keep this data.
What am I missing?

A hidden textbox is definitely the way to go here, so I'd suggest that your
JavaScript is where the problem lies.

Please show your JavaScript and how you call it.

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
Using runat="server" for the input box did the trick. I was unaware
you could get an input box to run at server. Thank you very much! I
do web development in an on again/ off again type manner so I seem to
forget as much as I learn!

For this comment though:
If your JavaScript is supposed to select all of the elements in a ListBox
but, in fact, clears the ListBox, then there is either a bug in your
JavaScript, or it's not running at the correct place in the page cycle. This
would need to run in the OnClientClick event of the <asp:Button /control
which does the postback.
Nothing in my code is clearing the list box. I have put breakpoints
through all my code and the listbox is cleared before ONE line of code
is executed. Something about .NET is clearing that listbox. So I
couldn't even get my code to execute because by the time it tried, the
list box was already cleared. This is perplexing to me. I'm not sure
I understand the purpose of values going away like that.

But it works now with the hidden text box option. So I'm happy.
Thanks again!
Nov 7 '08 #3
May be you misunderstood the advice.
I was told the issue is that the only thing retained in the list box
is the item selected. So I tried to write a javascript method that
You were told the right thing.
would select all of them when the user clicks the "Run Report"
button. It doesn't work, because I have to register the javascript
code in my code behind and it clears that listbox before it executes
even one line of code in the codebehind.
First of all, there is no need to write javascript in code behind.
You can write directly on aspx page with script tag.

Once you populate listbox (i.e. <selecttag) using javascript and select
all of them (make sure that listbox is multi-selectable), you can submit
it.

But remember, you can never access submitted code via listbox.
You need to access them from postback data. That data can be
got using Request.Forms["Listbox.UniqueID"] . This definitely
would contain all of the options.
Listbox is the best option, surely, unless you create your own custom
control with ajax support.

--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------

Nov 7 '08 #4
"Doogie" <dn******@dtgnet.comwrote in message
news:c7**********************************@v22g2000 pro.googlegroups.com...
Nothing in my code is clearing the list box. I have put breakpoints
through all my code and the listbox is cleared before ONE line of code
is executed. Something about .NET is clearing that listbox. So I
couldn't even get my code to execute because by the time it tried, the
list box was already cleared. This is perplexing to me. I'm not sure
I understand the purpose of values going away like that.
A ListBox will only "remember" pre-populated elements - client-side changes
made to the list of element will disappear as soon as the postback happens.
That's why you need to copy them into the hidden textbox *before* the
postback - that is what the <asp:Button />'s OnClientClick method is for...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #5
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:Ov**************@TK2MSFTNGP06.phx.gbl...
May be you misunderstood the advice.
I believe it is you who have misunderstood the question...
But remember, you can never access submitted code via listbox.
You need to access them from postback data. That data can be
got using Request.Forms["Listbox.UniqueID"] . This definitely
would contain all of the options.
No it wouldn't...

The OP is not trying to query which of a listbox's *pre-populated* elements
are *selected*...

Client-side changes to the contents of a listbox's elements collection will
not survive the postback - this is why the changes need to be copied into a
hidden field first...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #6
may be you have overused ASP.NET.
Server-side or client-side, all changes are posted back. it is HTTP
features. try to understand POST protocol.
The OP is not trying to query which of a listbox's *pre-populated*
elements are *selected*...
Browser doesn't remember, which of the elements are "pre-populated". HTML is
stateless. Hence pre or post population doesn't matter.
Come on, I have done it so many times.
--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------

Nov 7 '08 #7
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:eT**************@TK2MSFTNGP05.phx.gbl...
may be you have overused ASP.NET.
Server-side or client-side, all changes are posted back. it is HTTP
features. try to understand POST protocol.
1) Create an aspx page as follows:

<head>
<script type="text/javascript">
function addElements()
{
var lstBox = document.getElementById('<%=MyListBox.ClientID%>') ;
lstBox.options[lstBox.length] = new Option('First');
lstBox.options[lstBox.length] = new Option('Second');
lstBox.options[lstBox.length] = new Option('Third');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="MyListBox" runat="server" />
<input type="button" id="MyAddButton" value="Add elements"
onclick="addElements();" />
<asp:Button ID="MySubmitButton" runat="server" Text="Submit"
OnClick="MySubmitButton_Click" />
</form>
</body>

2) Add the appropriate server-side MySubmitButton_Click method and set a
breakpoint in it.

3) Launch the page in debug mode - how many elements does the listbox have?

4) Click the Add elements button - how many elements does the listbox have
now?

5) Hit the Submit button - when the code breaks in the MySubmitButton_Click
method, how many of the dynamically added listbox elements have survived the
postback...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #8
See My comments inline:-
1) Create an aspx page as follows:

<head>
<script type="text/javascript">
function addElements()
{
var lstBox = document.getElementById('<%=MyListBox.ClientID%>') ;
lstBox.options[lstBox.length] = new Option('First');
lstBox.options[lstBox.length] = new Option('Second');
lstBox.options[lstBox.length] = new Option('Third');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="MyListBox" runat="server" />
<input type="button" id="MyAddButton" value="Add elements"
onclick="addElements();" />
<asp:Button ID="MySubmitButton" runat="server" Text="Submit"
OnClick="MySubmitButton_Click" />
</form>
</body>
Created. I also set the property of Listbox as multiple selection in
designer.

2) Add the appropriate server-side MySubmitButton_Click method and set a
breakpoint in it.
Done.
3) Launch the page in debug mode - how many elements does the listbox
have?
None.
>
4) Click the Add elements button - how many elements does the listbox have
now?
3. "First", "Second", "Third"
5) Hit the Submit button - when the code breaks in the
MySubmitButton_Click method, how many of the dynamically added listbox
elements have survived the postback...?
I selected all three. at breakpoint checked property
Request.Form[1]. Its value is :-

"First","Second","Third" .

Actually this is the first time I am seeing how the multiple selectable
listbox/dropdown
are represented in HTTP Postback, although I knew that something like this
happens.

I would again say. Don't be spoilt by ASP.NET. Also try to know HTTP
protocol.
Why don't you yourself try it once ??
>

--
Mark Rae
ASP.NET MVP
http://www.markrae.net
--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
Nov 7 '08 #9
After I tested with your code, I can now confirmingly say that THIS WORKS.
and Doogie can use it. the list comes in comma separated string.

--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oo*************@TK2MSFTNGP06.phx.gbl...
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:eT**************@TK2MSFTNGP05.phx.gbl...
>may be you have overused ASP.NET.
Server-side or client-side, all changes are posted back. it is HTTP
features. try to understand POST protocol.

1) Create an aspx page as follows:

<head>
<script type="text/javascript">
function addElements()
{
var lstBox = document.getElementById('<%=MyListBox.ClientID%>') ;
lstBox.options[lstBox.length] = new Option('First');
lstBox.options[lstBox.length] = new Option('Second');
lstBox.options[lstBox.length] = new Option('Third');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="MyListBox" runat="server" />
<input type="button" id="MyAddButton" value="Add elements"
onclick="addElements();" />
<asp:Button ID="MySubmitButton" runat="server" Text="Submit"
OnClick="MySubmitButton_Click" />
</form>
</body>

2) Add the appropriate server-side MySubmitButton_Click method and set a
breakpoint in it.

3) Launch the page in debug mode - how many elements does the listbox
have?

4) Click the Add elements button - how many elements does the listbox have
now?

5) Hit the Submit button - when the code breaks in the
MySubmitButton_Click method, how many of the dynamically added listbox
elements have survived the postback...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #10
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:ug****************@TK2MSFTNGP06.phx.gbl...
I selected all three
Why did you do that? I didn't ask you to do that! Read the OP again - the
point of this is *not* to count the number of *selected* elements...

Try it again, but this time *don't* select any of the listbox elements...

Now what is the value of Request.Form[1]...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #11
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:Or**************@TK2MSFTNGP03.phx.gbl...
After I tested with your code, I can now confirmingly say that THIS WORKS.
and Doogie can use it. the list comes in comma separated string.
Don't select any of the dynamically added elements...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #12
oh, come on,
"So I tried to write a javascript method that
would select all of them when the user clicks the "Run Report"
button. "

That is a quote from Doogie. He tried to select it through javascript, but
still didn't get it to work.
You just need a trick to submit the data and I gave that trick.
WHY NOT SELECT DYNAMIC ELELMENT ?

--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:Or**************@TK2MSFTNGP03.phx.gbl...
>After I tested with your code, I can now confirmingly say that THIS
WORKS. and Doogie can use it. the list comes in comma separated string.

Don't select any of the dynamically added elements...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #13
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:ef*************@TK2MSFTNGP05.phx.gbl...
>>After I tested with your code, I can now confirmingly say that THIS
WORKS. and Doogie can use it. the list comes in comma separated string.

Don't select any of the dynamically added elements...

WHY NOT SELECT DYNAMIC [ELELMENT] ELEMENT ?
BECAUSE THAT'S NOT WHAT THE OP ACTUALLY WANTED TO DO!!!
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #14
BECAUSE THAT'S NOT WHAT THE OP ACTUALLY WANTED TO DO!!!
Oh, my god. Doogie tried it but could not get it right. Okay, let the doogie
reply to it. If it works for him, why to bother.

--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------

Nov 7 '08 #15
On Nov 7, 11:11*am, "Vinay Khaitan" <vkhai...@gmail.comwrote:
BECAUSE THAT'S NOT WHAT THE OP ACTUALLY WANTED TO DO!!!

Oh, my god. Doogie tried it but could not get it right. Okay, let the doogie
reply to it. If it works for him, why to bother.

--
Vinay Khaitan
[Windows Forms Layout Control]http://www.smart-components.com/
----------------------------------------------------------------
Wow. I didn't know you all had kept talking about this. I used the
runat="server" option on my hidden text box and I'm good with getting
what I need. The biggest issue I ran into is say I posted to the
server, but ran into some issue that the user needs to correct (i.e.
invalid number, missing some other value on the form, etc). The data
in the listbox is gone. So I reload it using the hidden text box.
However, once I reload it, it's now on the server and not on the
client. So say the user adds a couple more numbers and tries to go to
the server again and say another issue happens. The attempt to reload
it that time causes duplicate numbers because some of them didn't go
away that time (because they are on the server now). So to get around
it, the moment I go to the server, I clear the listbox completely on
my own, and load it with my hidden text box.

But in essence, I got everything working so I'm good. Thanks!
Nov 13 '08 #16

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

Similar topics

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)...
5
by: Bill | last post by:
I have have two list boxes. One is a listing of all possible variables. We'll call this listbox A. The other is a listing of all the selected variables. We'll call this listbox B. If a person...
2
by: mathieu cupryk | last post by:
I have problems with listboxes in the webform2.cs, the textboxes are working well when I do a click on next. I am missing something. It works with the textboxes. Here is the file: using System;...
3
by: jayderk | last post by:
Hello All, I am running in to a situation where the listbox is not refreshing for me. I am using a timer to cycle every second and call the timer_elapsed() event. in the time_elapsed event...
4
by: James Radke | last post by:
Hello, I am creating an owner draw listbox for a windows application. It is all working, except the performance is significantly slower than the standard listbox. Basically what I have done is...
6
by: Glen Wolinsky | last post by:
I have two virtually identical listbox controls on a tabPage in my application. I bind a small datatable to each one when a new project (my data) is selected from a treeview control (this works...
1
by: Crazy Cat | last post by:
Hi, I have a form with a databound listbox and combobox. The listbox items are filtered based on the selection for the combobox. If I click on the listbox to select an item, I can no longer...
2
by: Ian Semmel | last post by:
If I have a List<and a ListBox.DataSource = List then it initially displays OK, but if I change the order of the List, the displayed text in the ListBox doesn't. How do you make this happen ?
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
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
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
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
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...
0
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...
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,...
0
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...

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.