如何手动遍历添加DropDownList 有4个字段 name unit address telpone 如何手动遍历添加到DropDownList里我知道是用DropDownList.Items.Add 方法,但具体怎么写不知道了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 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);} 这样不行啊dt.Rows.Count是0,lt.Attributes["unit"] = dt.Rows[i]["unit"].ToString();也报错 如果你从数据库查询出来的的字段,放到DataSet里面。还可以这样做.DataSet ds = new DataSet();this.DropDownList1.DataSource =ds.Tables[0];this.DropDownList1.DataValueField = "字段";//显示的字段this.DropDownList1.DataTextField = "字段";//value值的字段this.DropDownList1.DataBind(); 绑定的方法我已经知道了 我用绑定的方法绑到DropDownList,怎样才能显示出字段名啊 例如 name unit address telpone 小红 小小公园 广州 2345678 这个方法不一定是最好的~1先把查询字段重新构建 datatable,datarow.把列替换为行。再绑定.应该还有更加好的方法 ie 头 ie 尾 制图 ASP.NET截取URL中的字符串 怎么样用正则来获取到"从哪到哪"的html代码? asp.net上传文件 用js实现显示一张图片怎么不管用??? 关于DataList的CheckBox的更新问题 高手请进,问题棘手 急问:radiobutton的问题!!!!! 一个datagird中关于Button的问题 高手救命!!!高分送上!! 关于RegisterStartupScript Asp.Net 给JS方法传值时值自动改变?!! 跪求高人指点
dropdownlist.Items.Add(new ListItem("name","nameId")
...
foreach(string str in 字段)
{
DropDownList.Items.Add(str);
}
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);
}
这样不行啊dt.Rows.Count是0,lt.Attributes["unit"] = dt.Rows[i]["unit"].ToString();也报错
还可以这样做.
DataSet ds = new DataSet();this.DropDownList1.DataSource =ds.Tables[0];
this.DropDownList1.DataValueField = "字段";//显示的字段
this.DropDownList1.DataTextField = "字段";//value值的字段
this.DropDownList1.DataBind();
小红 小小公园 广州 2345678
先把查询字段重新构建 datatable,datarow.
把列替换为行。
再绑定.
应该还有更加好的方法