DropdownList数据源

大家好,我有关于下拉列表的问题。 我正在使用数据源的下拉列表。 我怎样才能得到我select的价值?

// I need a if statement here because my programme doesn't know which value of dropdown list selected and I don't know how to use this with datasource. if(//if I select quiz 1 from dropdown list ,quiz 1 should list questions.) SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); string chooce = "Select Quiz from tblQuiz where Quiz=1 "; SqlCommand userExist = new SqlCommand(chooce, con); con.Open(); int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString()); if (temp == 1) { if (rbList.Items[0].Selected == true) { string cmdStr = "Select Question from tblQuiz where ID=1"; SqlCommand quest = new SqlCommand(cmdStr, con); lblque.Text = quest.ExecuteScalar().ToString(); con.Close(); } 

您可以通过使用List, Dictionary, Enum, DataSet DataTable以不同的方式绑定DropDownList。
主要你必须考虑三件事,而绑定下拉的数据源。

  1. DataSource – 数据集或数据表的名称或数据源
  2. DataValueField – 这些字段将被隐藏
  3. DataTextField – 这些字段将显示在dropdwon上。

您可以使用以下代码将下拉列表绑定到数据源作为数据表:

  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(); 

如果你想处理selectdropdownlist,那么你必须改变AutoPostBack="true"你可以使用SelectedIndexChanged事件来编写你的代码。

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string strQUIZ_ID=DropDownList1.SelectedValue; string strQUIZ_Name=DropDownList1.SelectedItem.Text; // Your code.............. } 
  protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { drpCategory.DataSource = CategoryHelper.Categories; drpCategory.DataTextField = "Name"; drpCategory.DataValueField = "Id"; drpCategory.DataBind(); } } 

参考这个链接的例子。 这可能对你有帮助。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.aspx

 void Page_Load(Object sender, EventArgs e) { // Load data for the DropDownList control only once, when the // page is first loaded. if(!IsPostBack) { // Specify the data source and field names for the Text // and Value properties of the items (ListItem objects) // in the DropDownList control. ColorList.DataSource = CreateDataSource(); ColorList.DataTextField = "ColorTextField"; ColorList.DataValueField = "ColorValueField"; // Bind the data to the control. ColorList.DataBind(); // Set the default selected item, if desired. ColorList.SelectedIndex = 0; } } void Selection_Change(Object sender, EventArgs e) { // Set the background color for days in the Calendar control // based on the value selected by the user from the // DropDownList control. Calendar1.DayStyle.BackColor = System.Drawing.Color.FromName(ColorList.SelectedItem.Value); } 

这取决于你如何设置下拉的默认值。 使用选定的值,但必须设置选定的值。 例如,我使用表/列表的名称和id字段填充数据源。 我将选定的值设置为id字段,显示名称。 当我select,我得到的ID字段。 我用它来search一个关系表并find一个实体/logging。