Home > Database > Mysql Tutorial > How Do I Retrieve Selected Values from a Data Source-Bound DropdownList?

How Do I Retrieve Selected Values from a Data Source-Bound DropdownList?

DDD
Release: 2024-12-29 19:01:14
Original
905 people have browsed it

How Do I Retrieve Selected Values from a Data Source-Bound DropdownList?

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:

  • DataSource: Specifies the data source (dataset, datatable, or custom object).
  • DataValueField: The field to use as the hidden value of each option.
  • DataTextField: The field to display as the visible text in the dropdown.

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();
Copy after login

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
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template