在vb.net中填充下拉列表 [英] populating dropdown list in vb.net

查看:416
本文介绍了在vb.net中填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从sql server数据库填充的下拉列表。填充列表不是问题,但有人知道如何填充listitem的值部分。

 < asp: dropdownlist id =colors> 
< listitem value =1> black< / listitem>
< listitem value =2> blue< / listitem>
< listitem value =3> orange< / listitem>
< listitem value =4> red< / listitem>
< listitem value =5> violet< / listitem>

当您填充时,您如何填充值= 1,2,3,4,5

解决方案

首先你必须建立你的选择语句

 从[表]中选择[ID],[价值] 

您可以将查询存储到变量中(我使用r作为返回)
然后您需要将其附加到下拉列表中

  DropDownList1.DataTextField = r.Value 
DropDownList1.DataValueField = r.ID
DropDownList1.Databind()

如果你真的需要循环,然后尝试这些行(不是代码不检查,只是一般的想法)。

 对于每个o作为r 
中的对象DropDownList1.Items.Insert(o.ID,新ListItem(o.Value,o.ID))
下一个

或者与DataReader(再次未经测试,但很接近)

 而DataReader.Read()
DropDownList1.Items.Insert(datareader(value),新ListItem(datareader(name),datareader(value))
结束while


i have a dropdownlist that populates from the sql server database. populating the list is not a problem, but does anyone know how to populate the value part of the listitem.

<asp:dropdownlist id="colors">
<listitem value="1">black</listitem>
<listitem value="2">blue</listitem>
<listitem value="3">orange</listitem>
<listitem value="4">red</listitem>
<listitem value="5">violet</listitem>

how do you populate the value=1,2,3,4,5 when you're populating from table in database?

解决方案

First you have to build your select statement

Select [ID], [Value] From [Table]

You would store your query into a variable (I use "r" for return) Then you need to attach it to the dropdown

DropDownList1.DataTextField = r.Value
DropDownList1.DataValueField = r.ID
DropDownList1.Databind()

If you really REALLY need to loop, then try something along these lines (not code is not checked, just a general idea.)

For Each o as object in r
  DropDownList1.Items.Insert(o.ID,new ListItem(o.Value,o.ID))
Next

Or with the DataReader (again, untested, but prolly close)

While DataReader.Read()
  DropDownList1.Items.Insert(datareader("value"),new ListItem(datareader("name"),datareader("value"))
End While

这篇关于在vb.net中填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆