<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行代码,老是理解不到

解决方案 »

  1.   

    DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段
    就是显示的字段 
    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("请选择", ""));
      

  2.   

    DataSet ds=new DataSet();
    ds="从数据库返回的dataset数据";
    DDfzname.DataSource=ds.Table[0];
    DDfzname.DataTextField="字段列";//也就是select查询语句中对应的列名,绑定在dropdownlist后,会显示在下拉框中。
    DropDownList1.DataValueField="字段列"";//也就是select查询语句中对应的列名,绑定在dropdownlist后,就是选择text值所对应的value值。
    DDfzname.DataBind();
      

  3.   


    DropDownList1.DataValueField  在 界面里就是 Value的值
       去看网页的 源代码 你就知道了。
      

  4.   


    楼上的能不能 帮我吧html的也修改下谢谢...this.DropDownList1.DataTextField = "显示的字段";
    this.DropDownList1.DataValueField = "值的字段";//2个字段在DataTable里都包含我老是看不懂这2行是什么玩意
      

  5.   

    你的查询方法可以这样写返回一个集合public static List<XX> GetXX(){};
    后台代码: List<XX> list = 逻辑层.GetXX();
    DropDownList1.DataSource = list;
    DropDownList1.DataValueField = "ItemName";
    DropDownList1.DataTextField = "id";
    DropDownList1.DataBind();
      

  6.   


    <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,"--请选择--");
      

  7.   

    假设你的数据库中有2个字段 一个是name  一个是value那么你就select * from table 这样 查出来的数据包含这两列的值那么你的下拉列表要显示name 这一列
    就设置
    this.DropDownList1.DataTextField = "name";//这个是你下拉时候显示的但是有的时候你要获取他的选中值this.DropDownList1.SelectedValue;
    这个值就是选中值 就是
    this.DropDownList1.DataValueField = "value";
      

  8.   

    选中值 和显示值?其实我要查出来的数据不就是name这个地段麽,为什么还要用多一个value这个字段呢?等10分钟,如果能回复就最好了,不能回复我就结贴了,谢谢了。
      

  9.   

    假如你查询的时候需要一个ID显示的时候是一个名字下拉显示的就是张三   对应ID1001
    李四绑定的”张三“就是text
    选择的时候 
    通过张三的userID 去数据库查this.DropDownList.SelectedValue这个值就是1001选张三 实际上是去取他在数据库里的ID。1001
      

  10.   

    this.DropDownList1.DataTextField = "显示的字段";
    this.DropDownList1.DataValueField = "值的字段";//这两个字段要包含在DataTable中 ,也就是你的sql语句要查询这两列 ,
    也可以说是数据库中表的字段名
      

  11.   


    this.DropDownList1.DataTextField =this.DropDownList1.DataValueField ="name";//也可以