views:

359

answers:

3

Hi,

foreach (Book b in o.list)
{
  ListBox_Items.Items.Add(b.Title);
}

After I do this, the titles are now showing up in the listbox.

When I make a selection (Single Mode), ListBox_Items (Screen) is highlighting the row selected, but event SelectedIndexChanged is not triggering.

protected void ListBox_Items_SelectedIndexChanged(object sender, EventArgs e)
{
  int i = ListBox_Items.SelectedIndex;
}

ID="ListBox_Items" runat="server" EnableViewState="False" Width="400px" Rows="25" onselectedindexchanged="ListBox_Items_SelectedIndexChanged"

Any ideas ?

Michael

Edit 1 : Thanks to everyone for helping out. Got it to work now. Anyway, I had to turn on EnableViewState to True too. Because I have a "Remove" button to remove items from the listbox control, if EnableViewState is False, whenever I clicked the Remove button, the listbox becomes empty again.

+5  A: 

Add AutoPostBack="True" in your aspx tag

Sergio
thanks a lot sergio
Michael Ellick Ang
A: 

Do you have anything to make the page post back to the server?

You may need either a submit button, or you can add the property AutoPostBack="true" to your ListBox control.

See this MSDN Article for more information.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx

Robin Day
A: 

Try the following code.

<asp:ListBox ID="ListBox_Items" 
             runat="server" 
             EnableViewState="False" 
             Width="400px" 
             Rows="25" 
             OnSelectedIndexChanged="ListBox_Items_SelectedIndexChanged"
             AutoPostBack="true"></asp:ListBox>
Ian Roke
hi ian, really appreciate your help
Michael Ellick Ang