You have to ensure that the ListBoxes
aren't databound on postbacks first. Otherwise you would fill them always with their default items and they would be unselected again.
So use the IsPostBack
property, e.g.:
protected void Page_Load(Object sendeer, EventArgs e)
{
if(!IsPostBack)
{
DataBindListBoxes();
}
}
You also have to make the button-click event-handler at least protected and add the event handler to the aspx markup(or programmatically in codebehind):
codebehind:
protected void btnallsd_Click(object sender, EventArgs e){//...}
aspx:
<asp:Button ID="btnallsd" OnClick="btnallsd_Click" ....
To find the selected items you could also use Linq:
protected void btnallsd_Click(object sender, EventArgs e)
{
var selected = lstsource.Items.Cast<ListItem>()
.Where(li => li.Selected);
while (selected.Any())
{
var item = selected.First();
lstdestination.Items.Add(item);
lstsource.Items.Remove(item);
}
lstdestination.ClearSelection();
}