2

I have two list boxes. One is populated on Page_Load and the other remains empty. The user has buttons for adding users from the first list to the second list and back. When the form is submitted, the second list is empty, like it was when it was sent to the client. Here is the JS code:

function add() {
        $('#AvailableUsers option:selected').appendTo('#SelectedUsers');
    }

    function addall() {
        $('#AvailableUsers option').appendTo('#SelectedUsers');
    }

    function remove() {
        $('#SelectedUsers option:selected').appendTo('#AvailableUsers');
    }

    function removeall() {
        $('#SelectedUsers option').appendTo('#AvailableUsers');
    }

How do I bring the client-side changes back to the server side?

Edit: Code for server-side:

bool canDismiss = chkCanDismiss.Checked;
        string messageText = tbMessageText.Text;
        PaymentsDataContext db = new PaymentsDataContext();
        foreach (ListItem li in SelectedUsers.Items)
        {
            UserMessages newMessage = new UserMessages();
            newMessage.userName = li.Text;
            newMessage.messageText = messageText;
            newMessage.dismissed = false;
            newMessage.canDismiss = canDismiss;
            db.UserMessages.InsertOnSubmit(newMessage);
        }
        db.SubmitChanges();
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133

2 Answers2

3

You have to append/store those items in the hidden field as well, then you can get the items from the hidden field on the server side.

This is because the changes you made at client side are not available on the server side.

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
1

If I read correctly you are trying to use the Users in "Selected Users" Server side. I would have an on form submit client side event that selects all the users in the SelectedUsers list. This eliminates the need for a hidden variable. NOTE use the following in conjunction with your existing jQuery

$(document).ready(function(){
     $(form).submit(function(){
        $('#SelectedUsers option').attr("selected","selected");    
        return true;
     });
});

EDIT In response to comment: When the selected users control is originally loaded on the page there are no items and nothing selected. With your current code, options are added to the selected users list, on the client side. Currently when the form is submitted these values are not selected and therefore not posted back to the server. To post the values back to the server they need to be selected first. What the above code should do is select the users options in the SelectedUsers list and post the selected values, with the rest of the form, back to the server when the form is submitted.

Edit 2, Server Side Code: You may need to access the values via the Request.Form object. With multiple controls like multiple select boxes, multiple values are passed as a comma separated string.

string rawSelUsers = Request.Form[SelectedUsers.ClientID];

You can the perform a split on this string to break it into an array if needed.

Jon P
  • 19,442
  • 8
  • 49
  • 72
  • Sorry, I don't get it. What should this do? – Elad Lachmi Jul 25 '11 at 06:16
  • I've added a return to the submit function to ensure the form submits. Can you provide more info on how this isn't working? Is the form submitting, how are you accessing the selected values for this control? – Jon P Jul 25 '11 at 06:37
  • I edited the post to show the server-side code. As for not working... I get an empty list on the server-side. – Elad Lachmi Jul 25 '11 at 06:45