1、C#ASP.NET DROPDOWNLIST控制元件系結資料庫,資料庫內容如下

2、前臺代碼如下:
<asp:DropDownList ID="DropDownList1" runat="server" style="z-index: 1; left: 360px; top: 80px; position: absolute; height: 35px; width: 220px; bottom: 343px">
</asp:DropDownList>
3、后臺代碼如下:
public static DataTable Table(string sql)
{
using (MySqlConnection conn = new MySqlConnection(ConnStr))
{
MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string sql = string.Format(" select * from table1");
DropDownList1.Items.Clear();
DropDownList1.DataSource = Table(sql);;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string ss1 = DropDownList1.SelectedValue.ToString();
}
4、網頁運行畫面
明明選中的是第二項

為什么打斷點,得到的始終是第一項的值呀!參見下圖:

請問:
DROPDOWMLIST控制元件系結資料庫,明明下拉選項選擇的是第2項或第3項,為什么用
DropDownList1.SelectedValue.ToString(); 讀取的始終是第1項的值呢?
uj5u.com熱心網友回復:
網上說:當對DropDownList進行資料系結后,要想實作獲取DropDownList中值必須設定AppendDataBoundItems屬性為True
我修改AppendDataBoundItems屬性為True,還是沒有解決。什么原因造成的呢?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/142067.html
標籤:C#
