ASP.net Javascript Listbox / Select Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smed
    New Member
    • May 2007
    • 6

    ASP.net Javascript Listbox / Select Problem

    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.c ount = 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.ht ml

    Edward
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    • smed
      New Member
      • May 2007
      • 6

      #3
      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?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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?

        Comment

        • smed
          New Member
          • May 2007
          • 6

          #5
          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

          Comment

          • menocomp
            New Member
            • Apr 2007
            • 5

            #6
            hi,
            use --> Request("ListBo xID")
            you will get all selected values with a Comma separated.
            bye

            Comment

            • smed
              New Member
              • May 2007
              • 6

              #7
              Originally posted by menocomp
              hi,
              use --> Request("ListBo xID")
              you will get all selected values with a Comma separated.
              bye
              :( Request("NameOf MyListBox") is returning Nothing / null in my debugger

              Comment

              • smed
                New Member
                • May 2007
                • 6

                #8
                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

                Comment

                • smed
                  New Member
                  • May 2007
                  • 6

                  #9
                  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("nameof hiddenlistbox") and i get a comma sperated list of all the values i need for the db insert.


                  Cheers for all the help

                  Comment

                  • akgiddings
                    New Member
                    • Nov 2008
                    • 1

                    #10
                    Just an interesting update - I found out menocomp's solution above DOES work - BUT ONLY IF you access the Request("ListBo xID") from within the Page_PreInit routine. - After that routine it will be nothing.

                    You will get Selected values comma separated.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      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.

                      [code=JavaScript]
                      <script type="text/javascript">
                      function AddElementToLis tBox(elementID, listboxID)
                      { var element = document.getEle mentById(elemen tID);
                      var listBox = document.getEle mentById(listbo xID);

                      var opt = document.create Element("option ");
                      opt.text = elementID;
                      opt.value = elementID;
                      opt.selected = false;

                      listBox.options .add(opt);
                      }
                      function selectAll(listb oxID)
                      { var listBox = document.getEle mentById(listbo xID);

                      for(var i=0; i< listBox.options .length;i++)
                      {
                      listBox.options (i).selected = true;
                      }
                      }
                      </script>
                      [/code]

                      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:

                      [code=asp]
                      <div style="float:le ft">
                      <asp:CheckBox ID="CheckBox1" runat="server" Text="Add Me 1" /><br />
                      <asp:CheckBox ID="CheckBox2" runat="server" Text="Add Me 2"/><br />
                      <asp:CheckBox ID="CheckBox3" runat="server" Text="Add Me 3"/><br />
                      </div>
                      <div style="float:le ft">
                      <select size="4" name="ListBox1" id="ListBox1" multiple="multi ple"> </select>
                      </div>
                      <asp:Button ID="doStuff" runat="server" style="clear:bo th" text="Do Stuff!" /><br />
                      <asp:Label ID="message" runat="server"> </asp:Label>
                      <div style="clear:bo th"></div>
                      [/code]

                      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.

                      [code=vbnet]
                      Private Listbox1 As ListBox

                      Private Sub ThePagePreInit( ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.PreInit
                      Listbox1 = New ListBox
                      Dim values As String = Request.Params( "ListBox1")
                      If String.IsNullOr Empty(values) = False Then
                      Dim listboxItems() As String = values.Split(", "c)
                      For Each i As String In listboxItems
                      Listbox1.Items. Add(i)
                      Next
                      End If
                      End Sub
                      [/code]

                      I added code that displays what items were in the ListBox when the button was clicked:
                      [code=vbnet]
                      Private Sub doStuff_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles doStuff.Click
                      Dim str As New StringBuilder
                      If Listbox1.Items. Count > 0 Then
                      For Each i As ListItem In Listbox1.Items
                      str.Append(" " + i.Text)
                      Next
                      End If
                      message.Text = "Items Were: " + str.ToString
                      End Sub
                      [/code]

                      And I added the JavaScript to the CheckBoxes so that they are added to the ListBox1 using JavaScript in my PreRender event:
                      [code=vbnet]
                      Private Sub JSTest_PreRende r(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.PreRender
                      CheckBox1.Attri butes.Add("onCl ick", "AddElementToLi stBox('" + CheckBox1.Clien tID + "','ListBox1'); ")
                      CheckBox2.Attri butes.Add("onCl ick", "AddElementToLi stBox('" + CheckBox2.Clien tID + "','ListBox1'); ")
                      CheckBox3.Attri butes.Add("onCl ick", "AddElementToLi stBox('" + CheckBox3.Clien tID + "','ListBox1'); ")
                      doStuff.Attribu tes.Add("onClic k", "selectAll('Lis tBox1');")
                      End Sub
                      [/code]

                      Cheers!

                      -Frinny

                      Comment

                      Working...