日期:2014-05-17  浏览次数:20534 次

下拉框绑定的问题
页面上有两个下拉框,后面一个下拉框的值是根据前面的值改变而变化。各位高手帮忙看看我的下面这段代码有什么不对,怎么绑定不到?
 protected void ddlAttribute_SelectedIndexChanged(object sender, EventArgs e)
{
string bigClassname=this.ddlAttribute.Text.ToString();
int itemid = cc.selectBigId(bigClassname);
string strSql = "select * from t_class where itemid='" + itemid + "'";
SqlDataAdapter dbAdapter = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
dbAdapter.Fill(ds, "t_class");
DropDownList1.DataSource = ds.Tables["t_class"].DefaultView;
DropDownList1.DataTextField = ds.Tables["t_class"].Columns["classname"].ToString();
DropDownList1.DataValueField = ds.Tables["t_class"].Columns["classid"].ToString();
DropDownList1.DataBind();
}
~~~~~~~~~~~~~~~~~~~~~~~·

------解决方案--------------------
DropDownList1.DataTextField ="classname"; 
 DropDownList1.DataValueField ="classid"; 

------解决方案--------------------
DataTextField 和DataValueField 直接用字符串就可以了
------解决方案--------------------

<asp:DropDownList ID="ddlAttribute" runat="server" Width="85px" DataSourceID="SqlDataSource1" DataTextField="itemname" DataValueField="itemname" AutoPostBack="True" Font-Size="12px"> 

------解决方案--------------------
加上 AutoPostBack="True" 和 OnSelectedIndexChanged="ddlAttribute_SelectedIndexChanged"