473,837 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2376
"Doogie" <dn******@dtgne t.comwrote in message
news:10******** *************** ***********@k1g 2000prb.googleg roups.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...@markNOSPA Mrae.netwrote:
"Doogie" <dnlwh...@dtgne t.comwrote in message

news:10******** *************** ***********@k1g 2000prb.googleg roups.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.Unique ID"] . 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******@dtgne t.comwrote in message
news:c7******** *************** ***********@v22 g2000pro.google groups.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******** ******@TK2MSFTN GP06.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.Unique ID"] . 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******** ******@TK2MSFTN GP05.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.getEle mentById('<%=My ListBox.ClientI D%>');
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="addEle ments();" />
<asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
OnClick="MySubm itButton_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.getEle mentById('<%=My ListBox.ClientI D%>');
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="addEle ments();" />
<asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
OnClick="MySubm itButton_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**@markNOSPA Mrae.netwrote in message
news:Oo******** *****@TK2MSFTNG P06.phx.gbl...
"Vinay Khaitan" <vk******@gmail .comwrote in message
news:eT******** ******@TK2MSFTN GP05.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.getEle mentById('<%=My ListBox.ClientI D%>');
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="addEle ments();" />
<asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
OnClick="MySubm itButton_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

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

Similar topics

17
3138
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:
5
3533
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 double-clicks on one of the variables in listbox A it "moves" it to listbox B. What is going on behind the scenes is that the table that holds all these variables has a yes/no field call Print. When the user double clicks on the value it fires a...
2
2123
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; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Drawing;
3
1976
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 Method I have code that checks to see if a DB table has been updated.. if so the new new data is uploaded to the listbox by calling lstbox1.datasource = myDBDataSet;
4
4356
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 added two new properties (full source below): ChangeBackgroundMember = a bound data field which contains a boolean as to whether this record should get a special background color ChangeBackgroundColor = the color to use when the above...
6
1338
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 fine). When I select the first project, the data is retrieved without a problem and the datatables are properly bound to both listboxes. The first listbox has a "SelectedIndexChanged" event wired to it which also works fine. Now, here's the...
1
2764
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 select the combobox or even exit the form. It's as if something won't let me take focus away from the listbox -- all I'm able to do is navigate within the listbox.. There is no error or exception thrown -- I just can't access anything outside of...
2
1614
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
4035
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
9843
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10577
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
10630
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
9407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7811
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
7005
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();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
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.