472,145 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 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 18299
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

Post your reply

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

Similar topics

4 posts views Thread by google | last post: by
7 posts views Thread by Colleyville Alan | last post: by
reply views Thread by Luis Esteban Valencia | last post: by
1 post views Thread by deepak | last post: by
reply views Thread by leo001 | last post: by

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.