C# 下拉列表数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14105265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:41:17  来源:igfitidea点击:

DropdownList DataSource

c#asp.net.netsql

提问by Alijohnn

Hi everyone I have problem about dropdown list. I am using dropdown list with datasource. How can I get that value which I selected ?

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

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

采纳答案by Arshad

You can bind the DropDownList in different ways by using List, Dictionary, Enum, DataSet DataTable.
Main you have to consider three thing while binding the datasource of a dropdown.

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

  1. DataSource - Name of the dataset or datatable or your datasource
  2. DataValueField - These field will be hidden
  3. DataTextField - These field will be displayed on the dropdwon.
  1. DataSource - 数据集或数据表或您的数据源的名称
  2. DataValueField - 这些字段将被隐藏
  3. DataTextField - 这些字段将显示在 dropdwon 上。

you can use following code to bind a dropdownlist to a datasource as a 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();

if you want to process on selection of dropdownlist, then you have to change AutoPostBack="true"you can use SelectedIndexChangedevent to write your code.

如果要处理下拉列表的选择,则必须更改AutoPostBack="true"可以使用SelectedIndexChanged事件来编写代码。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string strQUIZ_ID=DropDownList1.SelectedValue;
    string strQUIZ_Name=DropDownList1.SelectedItem.Text;
    // Your code..............
}

回答by Prissy

It depends on how you set the defaults for the dropdown. Use selected value, but you have to set the selected value. For instance, I populate the datasource with the name and id field for the table/list. I set the selected value to the id field and the display to the name. When I select, I get the id field. I use this to search a relational table and find an entity/record.

这取决于您如何设置下拉菜单的默认值。使用选定的值,但您必须设置选定的值。例如,我使用表/列表的名称和 ID 字段填充数据源。我将选定的值设置为 id 字段,并将显示设置为名称。当我选择时,我得到 id 字段。我用它来搜索关系表并找到一个实体/记录。

回答by dekdev

Refer to example at this link. It may be help to you.

请参阅此链接中的示例。它可能对你有帮助。

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

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

  }

回答by ibrahim ozboluk

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        drpCategory.DataSource = CategoryHelper.Categories;
        drpCategory.DataTextField = "Name";
        drpCategory.DataValueField = "Id";
        drpCategory.DataBind();
    }
}