ItemsSelected not working with ListBox

  • Thread starter Thread starter ProgrammerChicago
  • Start date Start date
P

ProgrammerChicago

I'm not sure if this is the result of the postback behavior or my own
code, but for some reason my onclick function is not detecting listbox
selections (It's meant to delete files uploaded to the server).

The RemoveFiles function is being executed, bu I have a feeling the
selections are being cleared beforehand.

There is a Page_Load function which repopulates the listbox. Could
this be getting in the way?


Here is the PageLoad...

void Page_Load(Object S, EventArgs E)
{
ListFiles();
}

And basically, here is my listbox:

<asp:ListBox ID="FB" Width="100%" SelectionMode="Multiple""
runat="server" Font-Size="Small" Rows="5" />

My button:

<asp:button OnClick="RemoveFiles" ID="RemoveButton" runat="server"
Text="Remove File(s)" />

Here is the function which is not seeing the item selections:

void RemoveFiles(object sender, EventArgs e)
{
int i = 0;
String filename = "";
for (i = 0; i < (FB.Items.Count); i++)
{
if (FB.Items.Selected) // FB is the
{ // never gets here...
filename = FB.Items.Text.ToString();
System.IO.File.Delete(Session["FilePath"] + "\\" +
filename);
}
}
}


Thanks...
 
Hi,

If you cleare the list and repopulate again (in the load) then the selections
are lost. As control events occur after load you cannot reach selected elements.

Regards, Alex
[TechBlog] http://devkids.blogspot.co
 
try something like this and see if it works

for (int i = 0; i < listBox.Items.Count; i++)
{
if (listBox.Items.Selected)
{
// List the selected items
listBox += listBox.Items.Value + ",";
}
}

this will get all of your selected items in your list box,
 
Back
Top