unable to retrieve listbox items on postback , items moved usingjavascript between 2 list boxes (sou

Discussion in 'ASP .Net' started by divya, May 27, 2008.

  1. divya

    divya Guest

    Hello,

    I used the below code to move selected items between two listboxes
    Left and Right:-

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    /*
    - Function MoveItems()
    - Direction: Specifies direction i.e. L = Left, R = Right
    - All: Move All items 1 = All, 0 = Selected
    */
    function MoveItems(Direction, All)
    {
    var LstLeft;
    var LstRight;
    var Removed = '';
    var i;

    if (Direction == 'R')
    {
    var LstLeft = document.getElementById("<
    %=LstLeft.ClientID %>");
    var LstRight = document.getElementById("<
    %=LstRight.ClientID %>");
    }
    else
    {
    var LstLeft = document.getElementById("<
    %=LstRight.ClientID %>");
    var LstRight = document.getElementById("<
    %=LstLeft.ClientID %>");
    }

    for (i=0; i<LstLeft.length; i++)
    {
    if (LstLeft.options.selected || All == 1)
    if(ItemExists(LstRight, LstLeft.options.value)
    == 0)
    {
    LstRight[LstRight.length] = new
    Option(LstLeft.options.text, LstLeft.options.value, true);
    Removed = Removed + LstLeft.options.value +
    ',';
    }
    }
    RemoveFromList(LstLeft, Removed);
    return false;
    }

    function RemoveFromList (Lst, Items)
    {
    var Removed = Items.split(',');
    var j;
    var x;
    for (j=0; j<Removed.length; j++)
    {
    for (x=0; x<Lst.length; x++)
    {
    if (Lst.options[x] != null && Lst.options[x].value
    == Removed[j])
    {
    Lst.options[x] = null;
    }
    }
    }
    }

    function ItemExists(Lst, value)
    {
    var Flag = 0;
    var i = 0;
    for (i=0; i<Lst.length; i++)
    {
    if (Lst.options.value == value)
    {
    Flag = 1;
    break;
    }
    }
    return Flag;
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox ID="LstLeft" runat="server"
    SelectionMode="multiple" Width="200px" Rows="5" >
    <asp:ListItem Value="0" Text="Farhan"></asp:ListItem>
    <asp:ListItem Value="1" Text="Moin"></asp:ListItem>
    <asp:ListItem Value="2" Text="Haroon"></asp:ListItem>
    <asp:ListItem Value="3" Text="Khurram"></asp:ListItem>
    <asp:ListItem Value="4" Text="Mansur"></asp:ListItem>
    </asp:ListBox>
    &nbsp;&nbsp;
    <asp:ListBox ID="LstRight" runat="server"
    SelectionMode="multiple" Width="200px" Rows="5">
    </asp:ListBox>
    <br />
    <asp:Button ID="BtnRight" runat="server" Text=">"
    OnClientClick="return MoveItems('R', 0);" />
    &nbsp;
    <asp:Button ID="BtnLeft" runat="server" Text="<"
    OnClientClick="return MoveItems('L', 0);"/>
    <asp:Button ID="BtnRightAll" runat="server" Text=">>"
    OnClientClick="return MoveItems('R', 1);" />
    &nbsp;
    <asp:Button ID="BtnLeftAll" runat="server" Text="<<"
    OnClientClick="return MoveItems('L', 1);" />
    </div>
    </form>
    </body>
    </html>


    Problem:-

    I am unable to retrieve the items of the listbox on postback which are
    filled using the javascipt code as shown above.
    I am able to retrieve all the other fields values on postback , but
    these two listboxes don't show the updated count and items list.The
    rightbox items count on postback is zero though the items are moved
    into it from the left listbox.
    I am using EnableEventValidation="false" .

    Please help me with how to retrieve the updated listbox items on
    postback.

    Thanks ,
    Divya
     
    divya, May 27, 2008
    #1
    1. Advertising

  2. divya

    Munna Guest

    Hi divya.

    I am not sure about what went wrong. please try the bellow
    technique...

    Please use a hidden input field in your page, while moving the items
    in right listbox in javascript
    save the itemvalues in a comma or semicolon separated way in the
    hidden field.
    After postback, In "Page_load" event check the hidden input's value,
    and modify both list box ....

    Best of luck

    Munna
    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
     
    Munna, May 28, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kay

    moving items between list boxes

    Kay, Feb 3, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,045
    =?Utf-8?B?QXZuZWVzaA==?=
    Feb 3, 2004
  2. Stefan Mueller
    Replies:
    5
    Views:
    12,526
    jamesxa
    Jun 16, 2009
  3. Kodiak

    Retrieve ListBox items

    Kodiak, Sep 15, 2006, in forum: C++
    Replies:
    2
    Views:
    527
    Victor Bazarov
    Sep 16, 2006
  4. Rakhi
    Replies:
    1
    Views:
    131
    Erwin Moller
    Apr 3, 2008
  5. dkomo872
    Replies:
    13
    Views:
    545
    dkomo872
    Aug 26, 2009
Loading...

Share This Page