By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

Listbox data going away

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.