views:

2627

answers:

2

how do i insert items into an html listbox from a database? im using asp c#. i cant make the listbox run at server because the application wont work if i do that. so i have to insert values from a database into an html listbox. I just need to display 1 column of data. cheers..

A: 

There are two ways of doing this I can think of:

First, you can place an <asp:Placeholder /> tag on the page and generate the listbox in code:

var select = new HtmlSelect() { Size = 5 };

//assuming the data has been placed in an IEnumarble
foreach (var item in items)
{
    select.Items.Add(new ListItem() { Value = item });
}
selectPlaceholder.Controls.Add(select);

Second, you could create a WebService or ashx handler to provide the data and populate the list from javascript.

Adam Lassek
+1  A: 

You could use a Literal, build the HTML for the listbox, and set the .Text of the Literal.

You could either string together the HTML for the listbox manually, or you could build a Listbox in C# and use something like this to have C# export the HTML string to the Literal.

Jon Smock