0

could any body explain me how can i save state of two list boxes on post back i am using jQuery of this kind i dont know on what event what should i do or where can i save view state or how can i use hiddenField to persist the state of both list box

<script language="javascript" type="text/javascript">
         $(document).ready(function() {
            //If you want to move selected item from fromListBox to toListBox
                $("#add").click(function() {
                    $("#"+'<%= fromListBox.ClientID %>'+" option:selected").appendTo("#"+'<%=toListBox.ClientID %>');
                });
                //If you want to move all item from fromListBox to toListBox
                $("#addAll").click(function() {
                    $("#"+'<%= fromListBox.ClientID %>'+" option").appendTo("#"+'<%=toListBox.ClientID %>');
            });
                //If you want to remove selected item from toListBox to fromListBox
                $("#remove").click(function() {
                    $("#"+'<%=toListBox.ClientID %>'+" option:selected").appendTo("#"+'<%= fromListBox.ClientID %>');
                });
                //If you want to remove all items from toListBox to fromListBox
                $("#removeAll").click(function() {
                    $("#"+'<%=toListBox.ClientID %>'+" option").appendTo("#"+'<%= fromListBox.ClientID %>');
                });

            });
                  </script>

<asp:ListBox ID="fromListBox" runat="server" SelectionMode="Multiple" Height="150px" Width="150px"  >

<asp:ListItem Text="Student Enrollment ID" Value="enrollment_no"></asp:ListItem> <asp:ListItem Text="Student Name" Value="first_name"></asp:ListItem> <asp:ListItem Text="Last Name" Value="last_name"></asp:ListItem> <asp:ListItem Text="Father Name" Value="father_name"></asp:ListItem>

</asp:Listbox>

<asp:ListBox runat="server" ID="toListBox" ></asp:ListBox>
kbrimington
  • 25,142
  • 5
  • 62
  • 74
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

1 Answers1

0

I think you just should use normal html listboxes, not asp.net server controls. Here are some tips about it. And here you can find decent code for jquery that will move items beetween listboxes.

I think you have three options:

  • Use normal HTML listboxes, write some javascript code, and manipulate with them on server using standard POST request.
  • Use UpdatePanel with your listboxes (you'll eliminate page flickering and you probably wont have to use jQuery or rewrite your existing code)
  • Find or write your own control that uses javascript to move items and manage viewstate itself

I would use HTML listboxes (select elements) to avoid viewstate issues. It'll save you a lot of time.

Community
  • 1
  • 1
Jarek
  • 5,885
  • 6
  • 41
  • 55
  • ok, you are true i can use update panel with list box but i wanted to learn it through the custom control. Could you tell me how can i integrate ajax to my project i never did before what do i need to do ? – NoviceToDotNet Aug 13 '10 at 15:20
  • Check this out: http://www.codeproject.com/KB/custom-controls/ListBoxComponent01.aspx But really, don not create custom control that you'll use once or twice in your project. It's better to just create ascx with some javascript. It's much faster. – Jarek Aug 13 '10 at 16:17