473,394 Members | 1,567 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,394 software developers and data experts.

ASP.net Javascript Listbox / Select Problem

6
I'm having problems with a postback event containing an empty listbox.

I have a page in which i populate a listbox via JavaScript, after which i want to submit this information to a database. Clicking the button to trigger my VB code the listbox.items.count = 0 and this is a found at the top of the page_load in the debugger. EnableViewState=True and i can't think what else my be causing it.

please any help is really appreciated

I think my problem is similar to this one
http://www.thescripts.com/forum/thread339231.html

Edward
May 29 '07 #1
10 18431
Plater
7,872 Expert 4TB
The problem is that since you are populating the listbox with javascript (happens only on client side) your backend (server side) has no knowledge of this and is retaining it's last known state for the object (which is empty) or possibly it gets re-initialized to empty in your page_load() function.

What you could maybe do is have a hidden field whose value is the same as the selected value of the listbox (use javascript to populate this and then use a SUBMIT type button). Really you just need that information getting back to your server side for processing.
May 29 '07 #2
smed
6
I'm not really after the selected value, its values i've added to the listbox. the listbox is empty at the start but then is populated by the data returned from a showModalDialog. Is there not a way to send the listbox content back in the postback?

Or even use a htmlSelect object instead?
May 29 '07 #3
Plater
7,872 Expert 4TB
All the contents of the listbox could go in a hidden field as a comma seperated list (or some other delimanting character that you can parse later on in your server code).

Does the listbox have to be populated by javascript? Can you not use a PostBack or anything to get your server-side code to populate it?
May 29 '07 #4
smed
6
annoying i dont think thats possible,

user goes down the page filling out values, gets to the listbox where they select more than one value using the page built for the Modal Dialog, doing a postback here to refresh my page emptys all the other values they have filled out
May 29 '07 #5
hi,
use --> Request("ListBoxID")
you will get all selected values with a Comma separated.
bye
May 29 '07 #6
smed
6
hi,
use --> Request("ListBoxID")
you will get all selected values with a Comma separated.
bye
:( Request("NameOfMyListBox") is returning Nothing / null in my debugger
May 29 '07 #7
smed
6
if i select a value, or select more than one then i do get the Value from the selected listbox items back. is there anyway now to make it select all automatically during the postback from the button event
May 29 '07 #8
smed
6
I think i have a working solution now.


when the javascript updates the listbox, i also update another listbox in a hidden div after which i then select all on. then during the postbacl i can select all as like said above with Request("nameofhiddenlistbox") and i get a comma sperated list of all the values i need for the db insert.


Cheers for all the help
May 29 '07 #9
Just an interesting update - I found out menocomp's solution above DOES work - BUT ONLY IF you access the Request("ListBoxID") from within the Page_PreInit routine. - After that routine it will be nothing.

You will get Selected values comma separated.
Nov 13 '08 #10
Frinavale
9,735 Expert Mod 8TB
I just ran through your problem and discovered that I could not add items to a .NET ListBox control and submit them to the server. As I suspected, an exception is thrown because ASP.NET could not validate the ListBox since it was modified.

Really you shouldn't be using a ListBox control for what you are trying to do. If you want to list all of the items checked consider using a TextBox, or a Label....

Despite this fact, I went on to solve the problem.

I created the following simple JavaScript functions. One adds the items to the ListBox and the other selects all the items in the ListBox (because as you discovered only selected items are submitted to the server). Please note that there's no code to remove the item from the list.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function AddElementToListBox(elementID,listboxID)
  3. {    var element = document.getElementById(elementID);
  4.      var listBox = document.getElementById(listboxID);
  5.  
  6.      var opt = document.createElement("option");
  7.      opt.text = elementID;
  8.      opt.value = elementID;
  9.      opt.selected = false;
  10.  
  11.      listBox.options.add(opt);
  12. }
  13. function selectAll(listboxID)
  14. {  var listBox = document.getElementById(listboxID);
  15.  
  16.    for(var i=0; i< listBox.options.length;i++)
  17.    {
  18.         listBox.options(i).selected = true;
  19.    }
  20. }
  21. </script>
  22.  
Since ListBoxes are rendered as <select> HTML elements in the browser I added a <select> item to my ASP code and gave it the id of "ListBox1". I also added a bunch of .NET check boxes to the page whose IDs are added to ListBox1 when checked, a button to submit the page, and a label to display the items that were in the ListBox when the button is clicked:

Expand|Select|Wrap|Line Numbers
  1.     <div style="float:left">
  2.         <asp:CheckBox ID="CheckBox1" runat="server" Text="Add Me 1" /><br />
  3.         <asp:CheckBox ID="CheckBox2" runat="server" Text="Add Me 2"/><br />
  4.         <asp:CheckBox ID="CheckBox3" runat="server" Text="Add Me 3"/><br />
  5.     </div>
  6.     <div style="float:left">
  7.        <select size="4" name="ListBox1" id="ListBox1" multiple="multiple"> </select>
  8.     </div>
  9.     <asp:Button ID="doStuff" runat="server" style="clear:both" text="Do Stuff!" /><br />
  10.     <asp:Label ID="message" runat="server"></asp:Label>
  11.     <div style="clear:both"></div>
  12.  
In my server side code (VB.NET) declared a .NET ListBox object which I filled with the selected items in ListBox1. I named this ListBox "ListBox1". I then filled it with the items I retrieved from the Request object in my Page PreInit event.

Expand|Select|Wrap|Line Numbers
  1.   Private Listbox1 As ListBox
  2.  
  3.      Private Sub ThePagePreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
  4.         Listbox1 = New ListBox
  5.         Dim values As String = Request.Params("ListBox1")
  6.         If String.IsNullOrEmpty(values) = False Then
  7.             Dim listboxItems() As String = values.Split(","c)
  8.             For Each i As String In listboxItems
  9.                 Listbox1.Items.Add(i)
  10.             Next
  11.         End If
  12.     End Sub
  13.  
I added code that displays what items were in the ListBox when the button was clicked:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub doStuff_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles doStuff.Click
  2.         Dim str As New StringBuilder
  3.         If Listbox1.Items.Count > 0 Then
  4.             For Each i As ListItem In Listbox1.Items
  5.                 str.Append(" " + i.Text)
  6.             Next
  7.         End If
  8.         message.Text = "Items Were: " + str.ToString
  9.     End Sub
  10.  
And I added the JavaScript to the CheckBoxes so that they are added to the ListBox1 using JavaScript in my PreRender event:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub JSTest_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.         CheckBox1.Attributes.Add("onClick", "AddElementToListBox('" + CheckBox1.ClientID + "','ListBox1');")
  3.         CheckBox2.Attributes.Add("onClick", "AddElementToListBox('" + CheckBox2.ClientID + "','ListBox1');")
  4.         CheckBox3.Attributes.Add("onClick", "AddElementToListBox('" + CheckBox3.ClientID + "','ListBox1');")
  5.         doStuff.Attributes.Add("onClick", "selectAll('ListBox1');")
  6.     End Sub
  7.  
Cheers!

-Frinny
Nov 13 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: google | last post by:
Hi Heres how the following code works. A user selects a number from a listbox, and then javascript generates that many textboxes. ie. If a user selects 5 from the listbox, then 5 text boxes...
7
by: Colleyville Alan | last post by:
I have an app in which users are displayed a list of mutual fund from which they can choose. There is a listbox embedded in a two-tabbed notebook control. When the form is initally opened, the...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
4
by: Bob P. | last post by:
Hello, I have a page with: * two side-by-side asp:listboxes and two arrow asp:buttons allowing users to add/remove email addresses between them -- very much like Outlook, where you have the...
0
by: Luis Esteban Valencia | last post by:
have a problem and I'm not sure how to handle/fix it. I have three listboxes on my page. The first listbox has a list of software products. When you select an item in the Products listbox, then...
0
by: deepak | last post by:
Hi all, i m transefering values from one listbox to another listbox using javascript(both are html controls), the first listbox is populating with database at page_load time and the second listbox...
1
by: deepak | last post by:
hi , i have 2 listbox and 2 buttons like add and remove, both listbox have multiple selection true so that i can either add or remove multiple selected items. the first listbox is populated with...
1
by: bill | last post by:
I need to be able to dynamically add items to a <asp:listbox> (or HTML <select runat=server>) using clientside javascript, and retrieve the items when the page is posted back. I use this code to...
15
by: mc | last post by:
I'm writing an app for managing Task Lists, I'm trying to add some controls to a form that I can use to link tasks, my original intention was to: - Add two list boxes, one listing "all Tasks"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.