Moving items in listboxes and capturing in server side
I am trying to move items between 2 listboxes using jQuery code
function move_list_items(sourceid, destinationid) {
$("#" + sourceid + " option:selected").appendTo("#" + destinationid);
}
//this will move all selected items from source list to destination list
function move_list_items_all(sourceid, destinationid) {
$("#" + sourceid + " option").appendTo("#" + destinationid);
}
and my listbox code is like this
<asp:ListBox ID="lstFirst" runat="server" Width="300px" Height="250px" SelectionMode="Multiple"></asp:ListBox>
<tr>
<td>
<p align="center">
<input id="btnRight" type="button" value=">" onclick="move_list_items('lstFirst','lstSecond');" />
</p>
<p align="center">
<input id="btnLeft" type="button" value="<" onclick="move_list_items('lstSecond','lstFirst');" />
</p>
<p align="center">
<input id="btnRightAll" type="button" value=">>" onclick="move_list_items_all('lstFirst','lstSecond');" />
</p>
<p align="center">
<input id="btnLeftAll" type="button" value="<<" onclick="move_list_items_all('lstSecond开发者_Go百科,'lstFirst');" />
</p>
</td>
</tr>
<asp:ListBox ID="lstSecond" runat="server" Width="300px" Height="250px" SelectionMode="Multiple"></asp:ListBox>
The items move fine, but I cannot access the moved items from the lstSecond listbox using lstSecond.items. The count is zero. How can I access the added items from codebehind for saving?
The thing is your server side control (ListBox) doesn't read it's state from client side html, it always gets populated on server side (either from Markup or Code behind).
So one way to achieve your goal is to store items (of each listbox) in a client side hidden field as soon as you change them by jQuery. Then in server side you need to populate your listboxes based on the value presented in the hidden field (if any).
精彩评论