ItemsSelected not working with ListBox

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...
 
A

Alex Meleta

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
 
S

Steve

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,
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top