DropDownList问题--急
各位,我在页面上使用了一个DropDownList,其各项是动态加入的,代码如下:
DataController dataControl = new DataController();
SqlConnection connection = dataControl.getConnection();
string sql = "select * from NewsClassInfo ";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "newsInfo ");
DataTable dataTable = dataSet.Tables[ "newsInfo "];
foreach (DataRow dataRow in dataTable.Rows)
{
kindDropDownList.Items.Add(new ListItem(dataRow[1].ToString(), dataRow[0].ToString()));
}
但是不知道为什么,当改变这个DropDownList的值时采用kindDropDownList.SelectedItem.Value 不能获得正确的值,永远都是第一条的值,为什么?
------解决方案--------------------代码贴全点,这个看了有点模糊,要么就是page_load下没加if(!ispostback){},要么就是你的循环不正确
------解决方案--------------------kindDropDownList.DataSource=dataSet;
kindDropDownList.DataTextField = "... ";
kindDropDownList.DataValueField = "... ";
kindDropDownList.DataBind();
你用这样的方式绑定看看
------解决方案--------------------if(!IsPostBack)
{
DataController dataControl = new DataController();
SqlConnection connection = dataControl.getConnection();
string sql = "select * from NewsClassInfo ";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "newsInfo ");
DataTable dataTable = dataSet.Tables[ "newsInfo "];
foreach (DataRow dataRow in dataTable.Rows)
{
kindDropDownList.Items.Add(new ListItem(dataRow[1].ToString(), dataRow[0].ToString()));
}
}
------解决方案--------------------另外这段代码放在if(!ispostback){}中
------解决方案--------------------if(!IsPostBack)
{