Listbox data going away

  • Thread starter Thread starter Doogie
  • Start date Start date
D

Doogie

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 <input> with 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?
 
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. <select> tag) 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.
 
Sounds reasonable.


That's standard behaviour for ListBoxes - changes made to their elements
collection via client-side JavaScript do not survive a postback.


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


Can you be more specific?


Yes you can - just give it a runat="server" tag...


That's because setting a webcontrol's Visible property to "true" prevents
that control from being rendered to the client browser.


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.

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!
 
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.
 
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 ??
 
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 said:
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...?
 
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/
 
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.
 
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.

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!
 
Back
Top