tags:

views:

216

answers:

2

How can i add anything user selected from dropdownlist to listbox using JQuery? and when i post the page i should be able to retrieve "id, name" from the listbox.

<asp:DropDownList ID="ddlPerson" DataSourceID="ods_person" 
                DataValueField="Id" DataTextField="Name" runat="server" Width="221px" /><br />

                <asp:ListBox ID="lstPerson" runat="server" Width="245px"  
                Font-Bold="true" ForeColor="Green" SelectionMode="Multiple">
                </asp:ListBox> <br>
A: 

When your asp.net dropdown list control renders, it will not look the same. You need to run the page first to see what id to select it by. Then use the $("#ddlPerson...").val() and .text() methods to access the data.

Zacho
for dropdownlist i get - ctl00$cphMaster$ddlPerson
Abu Hamzah
for listbox i get - ctl00_cphMaster_lstPerson
Abu Hamzah
This should do the trick. $("#ctl00$cphMaster$ddlPerson").change(function () { var newOption = new Option($(this).text(), $(this).text()); $("#ctl00_cphMaster_lstPerson").append(newOption); });
Zacho
A: 

To add the items to the dropdown you can use the following script

<script type="text/javascript">
    $(function ()
    {
        $('[id$=items]').change(function (e)
        {
            var option = $('<option/>').html($(e.currentTarget).val());
            $('[id$=listBox]').append(option);
        });
    });
</script>

BUT you will run into an issue. Items added to listbox will not be persisted back into the list box's "Items" on the server. You will need to add these values into a hidden field on the client and split up the values on the server.

Wallace Breza
i see the problem but also i see the problem of removing the items, i have a situation where the user can "Add" or "Remove" from from the listbox. is there any better way or should i use ajax updatepanel ? ;)
Abu Hamzah
I'm sure there are a bunch of jquery plugins that do this as it is a very common UI feature. Check out the jquery plugins site @ http://plugins.jquery.com/
Wallace Breza
its more pain than i thought... there should be a way... what plugin you want me to look for?
Abu Hamzah
Try this one out. http://plugins.jquery.com/project/DualListbox
Wallace Breza