<asp:DropDownList ID="DDfzname" runat="server">
</asp:DropDownList>这个是前台页面的代码。//这个是SQL语句
select names from Userinfo where names='"+names+"' order by id desc怎么绑定呢?哪个碰能帮我写下..
DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段 DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
这2行代码,老是理解不到
</asp:DropDownList>这个是前台页面的代码。//这个是SQL语句
select names from Userinfo where names='"+names+"' order by id desc怎么绑定呢?哪个碰能帮我写下..
DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段 DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
这2行代码,老是理解不到
就是显示的字段
DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
显示字段对应的值
this.DropDownList1.SelectedValue;的时候取的是这个值
select names from Userinfo where names='"+names+"' order by id desc
里取2个字段呗 protected void Page_Load(object sender, EventArgs e)
{
string sql = "select * from Userinfo where names='" + names + "' order by id desc";
this.DropDownList1.DataSource = ReturnDataTable(sql);
this.DropDownList1.DataTextField = "显示的字段";
this.DropDownList1.DataValueField = "值的字段";//2个字段在DataTable里都包含
this.DropDownList1.DataBind(); } public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
最后加个请选择this.DropDownList1.Items.Insert(0, new ListItem("请选择", ""));
ds="从数据库返回的dataset数据";
DDfzname.DataSource=ds.Table[0];
DDfzname.DataTextField="字段列";//也就是select查询语句中对应的列名,绑定在dropdownlist后,会显示在下拉框中。
DropDownList1.DataValueField="字段列"";//也就是select查询语句中对应的列名,绑定在dropdownlist后,就是选择text值所对应的value值。
DDfzname.DataBind();
DropDownList1.DataValueField 在 界面里就是 Value的值
去看网页的 源代码 你就知道了。
楼上的能不能 帮我吧html的也修改下谢谢...this.DropDownList1.DataTextField = "显示的字段";
this.DropDownList1.DataValueField = "值的字段";//2个字段在DataTable里都包含我老是看不懂这2行是什么玩意
后台代码: List<XX> list = 逻辑层.GetXX();
DropDownList1.DataSource = list;
DropDownList1.DataValueField = "ItemName";
DropDownList1.DataTextField = "id";
DropDownList1.DataBind();
<asp:DropDownList ID="DDfzname" runat="server"> </asp:DropDownList>//这个是SQL语句
string strSQL = "select id,ItemName from Userinfo where names='"+names+"' order by id desc";
DataSet ds = XXX(strSQL); //根据SQL获取数据集
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段
this.DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,"--请选择--");
就设置
this.DropDownList1.DataTextField = "name";//这个是你下拉时候显示的但是有的时候你要获取他的选中值this.DropDownList1.SelectedValue;
这个值就是选中值 就是
this.DropDownList1.DataValueField = "value";
李四绑定的”张三“就是text
选择的时候
通过张三的userID 去数据库查this.DropDownList.SelectedValue这个值就是1001选张三 实际上是去取他在数据库里的ID。1001
this.DropDownList1.DataValueField = "值的字段";//这两个字段要包含在DataTable中 ,也就是你的sql语句要查询这两列 ,
也可以说是数据库中表的字段名
this.DropDownList1.DataTextField =this.DropDownList1.DataValueField ="name";//也可以