I am trying to use jQuery to move items between one ASP.Net listbox
control and another. The jQuery works great but when I post back and
access the listboxes in my codebhind, the items remain in the same
listboxes as when the page was open. I am thinking this is because of
ASP.Net maintaining the view state on the server controls and that
this approach is not valid.

Can someone confirm if this is the case or if not, what I have to do
to make the code work. Here is the jQuery I am using:

        $('#add').click(function() {
        return !$('#ListBox1 option:selected').remove().appendTo
('#ListBox2');
        });
        $('#remove').click(function() {
        return !$('#ListBox2 option:selected').remove().appendTo
('#ListBox1');
        });

And here are the ASP controls:

        <asp:ListBox ID="ListBox1" runat="server" Height="171px"
            SelectionMode="Multiple" Width="152px">
            <asp:ListItem>Item 1-1</asp:ListItem>
            <asp:ListItem>Item 2-1</asp:ListItem>
            <asp:ListItem>Item 3-1</asp:ListItem>
            <asp:ListItem>Item 4-1</asp:ListItem>
        </asp:ListBox>

        <asp:ListBox ID="ListBox2" runat="server" Height="171px"
            SelectionMode="Multiple" Width="152px">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
        </asp:ListBox>

Here is my codebhind on a button that is used to read the modified
lists and take some action (in this case just report back the list
items):

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim strResult As String = "LB1: "
        Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1
            strResult += ListBox1.Items(i).Text & "/"
        Next
        strResult += " ** LB2: "
        For i = 0 To ListBox2.Items.Count - 1
            strResult += ListBox2.Items(i).Text & "/"
        Next
        Result.Text = strResult
    End Sub

As I mentioned, the jQuery works great. I can move item s from ListBox
1 to ListBox2 but the displayed lists are not the ones I can access in
the code behind.

TIA
John

Reply via email to