我有以下代码填充下拉列表:
string strConn = ConfigurationManager.ConnectionStrings["LiSQL"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConn))
{
DataSet ds = new DataSet();
using (SqlDataAdapter myda = new SqlDataAdapter("SELECT [PostAbbrev], [State] FROM [States]", con))
{
myda.Fill(ds);
ddlState.DataSource = ds;
}
}
ddlState.DataTextField = "State";
ddlState.DataValueField = "State";
ddlState.DataBind();
我的下拉代码是这样的:
<asp:DropDownList ID="ddlState" runat="server" DataTextField="States" DataValueField="States">
</asp:DropDownList>
当我选择说德克萨斯时,该值仍默认为第一个条目阿拉巴马州:这是我为检索该值所做的:
string state = ddlState.SelectedItem.Text;