By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

unable to retrieve listbox items on postback , items moved usingjavascript between 2 list boxes (source and target )

P: n/a
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[i].selected || All == 1)
if(ItemExists(LstRight, LstLeft.options[i].value)
== 0)
{
LstRight[LstRight.length] = new
Option(LstLeft.options[i].text, LstLeft.options[i].value, true);
Removed = Removed + LstLeft.options[i].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[i].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
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.