Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropdownList数据源_C#_Asp.net_.net_Sql - Fatal编程技术网

C# DropdownList数据源

C# DropdownList数据源,c#,asp.net,.net,sql,C#,Asp.net,.net,Sql,大家好,我对下拉列表有问题。我正在使用下拉列表和数据源。我如何获得我选择的值 // 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

大家好,我对下拉列表有问题。我正在使用下拉列表和数据源。我如何获得我选择的值

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

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

您可以使用
列表、字典、枚举、数据集DataTable
以不同的方式绑定DropDownList
在绑定下拉列表的数据源时,必须考虑三件事。< /P>
  • DataSource—数据集或数据表或数据源的名称
  • DataValueField-这些字段将被隐藏
  • DataTextField-这些字段将显示在dropdwon上 您可以使用以下代码将dropdownlist作为
    数据表
    绑定到数据源:

      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();
    
    如果要在选择dropdownlist时进行处理,则必须更改
    AutoPostBack=“true”
    您可以使用
    SelectedIndexChanged
    事件来编写代码

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

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


    您列出的代码与您提出的问题不匹配。我们需要知道下拉列表的名称/id、您使用的数据源的类型,以及您将数据源绑定到下拉列表的方式,以便有效地回答这个问题。请稍作解释。看看
    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);
    
      }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            drpCategory.DataSource = CategoryHelper.Categories;
            drpCategory.DataTextField = "Name";
            drpCategory.DataValueField = "Id";
            drpCategory.DataBind();
        }
    }