[...]
public DataSet ReturnPromoMagazinesDs()
{
MySql.Data.MySqlClient.MySqlConnection mysqlConnection = new MySql.Data.MySqlClient.MySqlConnection(this.connectionString);
MySql.Data.MySqlClient.MySqlCommand mysqlCommand = new MySql.Data.MySqlClient.MySqlCommand("SELECT id, magazine_name FROM `magazines`", mysqlConnection);
MySql.Data.MySqlClient.MySqlDataAdapter mysqlAdaptor = new MySql.Data.MySqlClient.MySqlDataAdapter(mysqlCommand);
DataSet ds = new DataSet();
mysqlAdaptor.Fill(ds, "magazines");
return ds;
}
[...]
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = coreObject.ReturnPromoMagazinesDs();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = ds.Tables["magazines"].Columns["magazine_name"].ColumnName;
DropDownList1.DataValueField = ds.Tables["magazines"].Columns["id"].ColumnName;
DropDownList1.DataBind();
}
[...]
<asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist>
上面的代码工作正常,直到我获取始终为 1(表中的第一个值)的 DropDownList1.SelectedValue 。表中的值并不是造成这种情况的原因,如果我手动将项目添加到 DropDownList,一切都会正常。什么会导致这种情况?
最佳答案
当您将 Page_Load 方法中的所有代码包装在以下内容中时,它是否可以正常工作:
if (!IsPostBack) { /* Code as in the original post */ }
关于c# - ASP.NET DropDownList 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1388754/