0

DataValueField的. DataTextField_ DropDownList数据来自 a DataSet,我希望将我dropdownlist的文本字段显示Dataset为预选文本。由 a 中的Dataset数据填充mysql table,包含“ id”和“ text”。DropDownList代码是:

<asp:DropDownList runat="server" DataValueField="id" 
DataTextField="text" ID="statusList" CssClass="viewItemRight" 
AutoPostBack="true"></asp:DropDownList>

如果没有DataValueField-Tag 则DropDownList正确显示 my 中的文本值作为 myDataSet中的预选文本DropDownList。但是,如果我DataValueField在.DataTextFieldDropDownList

数据的代码是:

//load statusList
            cmd = new MySqlCommand();
            cmd.CommandText = "SELECT * FROM statuslist WHERE active = '1' ORDER BY sorting ASC";
            cmd.Connection = con;
            sda = new MySqlDataAdapter(cmd);
            ds = new DataSet();
            sda.Fill(ds);
            statusList.DataSource = ds;
            statusList.DataBind();
            statusList.Items.Insert(0, " ");

如何同时使用 theDataValueField和 the DataTextField

4

1 回答 1

0

尝试这样的事情

代码:

DataTable dt = populatedd();
   statusList.DataSource = dt;
   statusList.DataTextField = "name";
   statusList.DataValueField = "id";
   statusList.DataBind();

    public DataTable populatedd()
    {
        string myQuery = "select id,name from yourtable order by name";
        SqlDataAdapter dap = new SqlDataAdapter(myQuery, con);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        return ds.Tables[0];
    }

标记

<asp:DropDownList runat="server" ID="statusList" CssClass="viewItemRight" 
AutoPostBack="true"></asp:DropDownList>
于 2013-04-12T12:03:50.333 回答