[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading listitem values populated using javascript

Posted on 2006-05-31
4
Medium Priority
?
562 Views
Last Modified: 2008-02-26
The following code produces 2 listboxes, 1 that is populated and 1 that is empty.  If you select some values from the left list and press Add, the values copy across to the other box.  When the form is submitted to the server however, any values populated to the second listbox via javascript are not recognised.  The sendMessage subroutine does not read the values in the listbox when it is submitted.

Can anyone explain why this is, and offer a solution?


<%@ Page Language="VB" Trace="true" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
      Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            For i As Integer = 0 To 10
                  Dim li As New ListItem("Value: " & i, i)
                  lbx_recipientChoice.Items.Add(li)
            Next
      End Sub

      Sub sendMessage(ByVal sender As Object, ByVal e As EventArgs)
            Trace.Warn("Sending...")
            For Each li As ListItem In chosenFew.Items
                  If li.Selected Then
                        Trace.Warn(li.Value & ": " & li.Text)
                  End If
            Next
      End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
      <script type="text/javascript">
            function CopyValues(srcObj, trgObj) { // copy listbox values from one list to another
             var ix=0;
             var cnt = 0;
             var tcnt = 0;
             if (trgObj.options) {
                  tcnt = trgObj.options.length;
             }
             for (ix=0; ix < srcObj.options.length; ix++) {
                    if (srcObj.options[ix].selected) {
                           var valueFound = 0;
                              for (var tx=0; tx < trgObj.options.length; tx++) {
                                     if (trgObj.options[tx].value.match (srcObj.options[ix].value)) {
                                            valueFound = 1;  
                                            break;
                                     }
                              }
                           if (!valueFound) {
                                    trgObj.options[tcnt++] = new Option (srcObj.options[ix].text,srcObj.options[ix].value);
                           }
                           cnt++;
                    }
             }
             trgObj.options.length  = tcnt;
             cnt = 0;
      }

      function removeValues(srcObj){ // removes values added by above function
            for (ix=srcObj.options.length-1; ix >= 0; ix--) {
                  if (srcObj.options[ix].selected) {
                        srcObj.options[ix] = null;
                  }
            }
      }

      function selectAllRecipients(srcObj){ // cycle through recipient box and select every option so values are submitted
            if (srcObj) {
                  for (i=0;i<srcObj.length;i++)
                  {
                        srcObj.options[i].selected = true;
                  }
            }
      }
      </script>
</head>
<body>
    <form id="Form1" runat="server">
            <table>
                  <tr>
                        <td>To:</td>
                        <td><asp:ListBox ID="lbx_recipientChoice" runat="server" SelectionMode="Multiple" Rows="6" CssClass="selectWide"  />
                              <asp:Button ID="copyBtn" Text="Add" runat="server" CssClass="pushbutton" OnClientClick="CopyValues (this.form.lbx_recipientChoice, this.form.chosenFew); return false;" /></td>
                        <td><asp:ListBox ID="chosenFew" runat="server" SelectionMode="Multiple" Rows="6" CssClass="selectWide" />
                              <asp:Button ID="removeBtn" runat="server" Text="Remove" CssClass="pushbutton" OnClientClick="removeValues (this.form.chosenFew); return false;" /></td>
                  </tr>
                  <tr>
                        <td>Message:</td>
                        <td colspan="2"><asp:TextBox ID="tbx_send_message" runat="server" TextMode="MultiLine" cssClass="expandable" /></td>
                  </tr>
                  <tr>
                        <td></td>
                        <td colspan="2"><asp:Button ID="sendMsg" runat="server" Text="Send Message" CssClass="pushbutton" OnClientClick="selectAllRecipients(this.form.chosenFew);" onclick="sendMessage"/></td>
                  </tr>
            </table>            
    </form>
</body>
</html>
0
Comment
Question by:Rouchie
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:deighton
deighton earned 400 total points
ID: 16797264
The explanation could be that the items added to the list box are not posted back.

You could add an asp.net textbox, then add data to this using javascript, then this data will be posted back and you could parse out the added item data.  You'd probably want to hide the text box (possibly put it on a panel and set the panel to .display='none'; using java script
0
 
LVL 5

Accepted Solution

by:
vinodhsomasekharan earned 1600 total points
ID: 16797539
Hai,

I have just tried using an hidden box which stores the selected value and Text in the listbox "chosenFew"
Loop through the  listbox "chosenFew" and store the values to the hidden box "hdnSelectedValues"
It worked for me..


function selectAllRecipients(srcObj)
{
   var delimiter='###';
       var delimiter1='***';
       var selItemValues='';
       srcObj=document.getElementById('chosenFew');
        if (srcObj) {
               for (i=0;i<srcObj.length;i++)
               {
                    selItemValues = selItemValues + srcObj.options[i].value + delimiter1 + srcObj.options[i].text  + delimiter ;
               }
          }
          document.getElementById('hdnSelectedValues').value = selItemValues;
  // So your values will be like this  1***value 1:###3***###..etc
}

ServerSide:-
=======

  Sub sendMessage(ByVal sender As Object, ByVal e As EventArgs)

        Dim selValues() As String
        hdnSelectedValues.Value = hdnSelectedValues.Value.TrimEnd("#")
        selValues = Regex.Split(hdnSelectedValues.Value, "###")

        For i As Integer = 0 To selValues.Length - 1
            Dim selIndividualValues() As String

            selIndividualValues = Regex.Split(selValues(i), "\*\*\*")
            Trace.Warn(selIndividualValues(0) & ": " & selIndividualValues(1))

        Next

    End Sub


Regards
Vinu

0
 
LVL 25

Author Comment

by:Rouchie
ID: 16799246
Thanks for that suggestion.  I'll try it out tomorrow and let you know the outcome...!
0
 
LVL 25

Author Comment

by:Rouchie
ID: 16806669
That works great.  Thank you for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question