从数据库插入html列表框项

时间:2021-05-12 11:14:22

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

如何从数据库中将项目插入到html列表框中?即时通讯使用asp c#。我不能使列表框在服务器上运行,因为如果我这样做,应用程序将无法工作。所以我必须将数据库中的值插入到html列表框中。我只需要显示1列数据。干杯..

2 个解决方案

#1


1  

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

您可以使用Literal,为列表框构建HTML,并设置Literal的.Text。

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.

您可以手动将列表框的HTML串起来,也可以在C#中构建一个Listbox,并使用类似的东西让C#将HTML字符串导出到Literal。

#2


0  

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.

其次,您可以创建WebService或ashx处理程序来提供数据并从javascript填充列表。

#1


1  

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

您可以使用Literal,为列表框构建HTML,并设置Literal的.Text。

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.

您可以手动将列表框的HTML串起来,也可以在C#中构建一个Listbox,并使用类似的东西让C#将HTML字符串导出到Literal。

#2


0  

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.

其次,您可以创建WebService或ashx处理程序来提供数据并从javascript填充列表。