private void ddlDiarySortBind()
    {
        try
        {
            ddlDiarySort.Items.Clear();
            ddlDiarySort.Items.Add(new ListItem("-请选择文章类别-", "0"));
            sqlConn mySqlConn = new sqlConn(); //数据库连接
            string str_QSL = "Select * From 表";
            SqlConnection MyConn = new SqlConnection(mySqlConn.MyConnection);
            SqlCommand MyComm = new SqlCommand(str_QSL, MyConn);
            MyConn.Open();
            SqlDataReader ds = MyComm.ExecuteReader();
            while (ds.Read())
            {
                ddlDiarySort.Items.Add(new ListItem(ds["Title"].ToString(), ds["ID"].ToString()));
            }
            ds.Close();
            MyConn.Close();
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message);
            Response.End();
        }
    }

解决方案 »

  1.   

    假定你的表中包含:Name和ID字段
    需要绑定的控件是:listMyCtrlwhile(reader.Read())
    {
        ListItem oItem = new ListItem();
        oItem.Text = dr["Name"].ToString();
        oItem.Value = dr["ID"].ToString();
        listCtrl.Items.Add(oItem);
    }
    reader.Close();
      

  2.   

    if(reader.Read())
    {
        DropDownList1.DataSource = null;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
    }
    reader.Close();
    也可以使用楼上方法
      

  3.   

    用Reader读出来是独占方法,可以尝试读出为DataSet或DataTable
    利用DropDownList.DataSource = DataTable;
    DropDownList.DataTextField = "Name";
    DropDownList.DataValueField = "ID";
    DropDownList.DataBind();
    来实现2个绑定
      

  4.   

    来晚了........最好用SqlDataSource这个控件.....不用写代码...先加一个SqlDataSource,配置好数据源,,然后加一个DropDownList,选项这个数据源...就OK了....最简单而有效的方法..
      

  5.   

    如果两个DropDownList绑定的是不同的字段,可以用DataReader
    如果两个DropDownList绑定的是相同的字段,可以用DataSet
      

  6.   

    呵呵,不用这么麻烦吧,我发现一个小技术,在DDP中有一个SelectValue这个属性,他不在智能提示里,但是可以用。我写一个小例子大家参考。 我用了一个ObjDataSource,我想类里面的东西就不用我写了吧,用一个List返回到数据源里就OK了吧,我觉得这还是比较简单的一种方法。<asp:DropDownList ID="categoryselect" runat="server" SelectedValue='<%# Bind("TemplateCategoryID") %>' AppendDataBoundItems="true" DataSourceID="TCategoryBLL" DataTextField="CategoryName" DataValueField="CategoryID">
                                                            <asp:ListItem Selected="true" Text="请选择" Value="-1"></asp:ListItem>
                                                        </asp:DropDownList>
                                                        <asp:RangeValidator ID="rv1" runat="server" ControlToValidate="categoryselect" ErrorMessage="请正确选择分类" MinimumValue="0" MaximumValue="999" Type="Integer" SetFocusOnError="true" ></asp:RangeValidator>
                                                        <asp:ObjectDataSource ID="TCategoryBLL" runat="server" SelectMethod="GetAllCategory" TypeName="TemplateCategory"></asp:ObjectDataSource>
    直接给DropDownList的SelectedValue绑定到字段上就行了,这个属性不在智能提示里,但可以这样用