您可以使用来以不同的方式绑定DropDownList List, Dictionary, Enum, DataSet DataTable
。 主要在绑定下拉数据源时您必须考虑三件事。
您可以使用以下代码将下拉列表作为数据绑定到数据源datatable
:
sqlConnection con = new sqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
sqlCommand cmd = new sqlCommand("Select * from tblQuiz", con);
sqlDataAdapter da = new sqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "QUIZ_Name";
DropDownList1.DataValueField = "QUIZ_ID"
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
如果要处理选择下拉列表,则必须进行更改,然后AutoPostBack="true"
可以使用SelectedIndexChanged
event编写代码。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strQUIZ_ID=DropDownList1.SelectedValue;
string strQUIZ_Name=DropDownList1.SelectedItem.Text;
// Your code..............
}