Getting Selected Values from DropdownList with DataSource
Q: How to retrieve the selected value from a dropdown list populated with a data source?
A: Utilizing a data source in a dropdown list offers various binding options. However, understanding three key concepts is essential:
To bind a dropdown list to a data table data source, follow these steps:
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();
When you select an option from the dropdown list, the SelectedIndexChanged event is triggered. Use this event to process the selection:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string strQUIZ_ID=DropDownList1.SelectedValue; string strQUIZ_Name=DropDownList1.SelectedItem.Text; // Your code to process the selected value }
The above is the detailed content of How Do I Retrieve Selected Values from a Data Source-Bound DropdownList?. For more information, please follow other related articles on the PHP Chinese website!