2009-09-07 140 views
0
[...] 
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> 

上面的代碼工作正常,直到我取了DropDownList1.SelectedValue這始終是1(從表中的拳頭值)。表中的值不是責任,如果我手動添加項目到DropDownList,一切工作正常。什麼會導致這種情況?ASP.NET DropDownList的問題

回答

2

它,當你在下面的包裝所有的代碼在Page_Load方法中正常工作:

if (!IsPostBack) { /* Code as in the original post */ } 
0

不知道如果事情是迷路在transalation但即使你可以簡化以下行,其不解決問題。

DropDownList1.DataTextField = "magazine_name"; 

DropDownList1.DataValueField = "id"; 
+0

兩種方式產生相同的結果。即使我在droptdownlist.datasource = ds之前或之後使用dropdown.datetextfiel =「myfield」和dropdown.datatextvaluefield =「id」 – EsiX 2009-09-07 11:16:11