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

D

divya

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
 
M

Munna

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top