读书人

怎么手动遍历添加DropDownList

发布时间: 2012-01-03 22:16:06 作者: rapoo

如何手动遍历添加DropDownList
有4个字段 name unit address telpone 如何手动遍历添加到DropDownList里
我知道是用DropDownList.Items.Add 方法,但具体怎么写不知道了

[解决办法]

C# code
dropdownlist.Items.Add(new ListItem("name","nameId")...
[解决办法]
用foreach循环
foreach(string str in 字段)
{
DropDownList.Items.Add(str);
}
[解决办法]
一个DropDownList里面放四个字段打算怎么显示?
[解决办法]
dropdownlist.Items.Add(new ListItem("name","nameId")

[解决办法]
不知道楼主是不是想ADD到页面的所有DropDownList
[解决办法]
C# code
DataTable dt = new DataTable();dt=这里把你返回的table放进去for (int i = 0; i < dt.Rows.Count; i++){   ListItem lt = new ListItem();   lt.Text = dt.Rows[i]["name"].ToString();   lt.Value = dt.Rows[i]["name"].ToString();   lt.Attributes["unit"] = dt.Rows[i]["unit"].ToString();   lt.Attributes["address"] = dt.Rows[i]["address"].ToString();   lt.Attributes["telpone"] = dt.Rows[i]["telpone"].ToString();   dropdownlist.Items.Add(lt);}
[解决办法]
如果你从数据库查询出来的的字段,放到DataSet里面。
还可以这样做.
DataSet ds = new DataSet();

this.DropDownList1.DataSource =ds.Tables[0];
this.DropDownList1.DataValueField = "字段";//显示的字段
this.DropDownList1.DataTextField = "字段";//value值的字段
this.DropDownList1.DataBind();
[解决办法]
这个方法不一定是最好的~1
先把查询字段重新构建 datatable,datarow.
把列替换为行。
再绑定.
应该还有更加好的方法

读书人网 >asp.net

热点推荐