Click here to Skip to main content
15,935,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 10 listboxes in my aspx page. For all 10 listboxes same function is used. For some buttons I want to add listbox data to grid. Can you help me? My javascript code is shown below:
<script language="javascript" type="text/javascript" >
    function MoveItem(ctrlSource, ctrlTarget) {
       var Source = document.getElementById(ctrlSource);
       var Target = document.getElementById(ctrlTarget);

       if ((Source != null) && (Target != null)) {
           while ( Source.options.selectedIndex >= 0 ) {
               var newOption = new Option(); // Create a new instance of ListItem
               newOption.text = Source.options[Source.options.selectedIndex].text;
               newOption.value = Source.options[Source.options.selectedIndex].value;

               Target.options[Target.length] = newOption; //Append the item in Target
               Source.remove(Source.options.selectedIndex);  //Remove the item from Source

I tried the above code to move items between listbox using html input button. The problem is that when I am trying to save listbox.items.count is giving 0. Can anyone tell me why this is happening? And also, when post back occurs, the listbox items are lost.
Updated 6-Jan-11 1:26am

1 solution

Remember that the client (browser) and server are disconnected. The server has no idea what has happened on the client unless you tell it. When the page is rendered the listboxes are bound to some data, which is added to page's viewstate, that's how ASP.NET maintains state across requests. Adding or removing items directly from/to the DOM on the client side does not update the viewstate so the server knows nothing about those actions.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900