【问题标题】:DropdownList DataSource下拉列表数据源
【发布时间】:2012-12-15 19:54:19
【问题描述】:

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

// 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、您使用的数据源类型以及您将数据源绑定到下拉列表的方式,以便有效地回答这个问题。

标签: c# asp.net .net sql


【解决方案1】:

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

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

您可以使用以下代码将下拉列表作为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();

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

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

【讨论】:

    【解决方案2】:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            drpCategory.DataSource = CategoryHelper.Categories;
            drpCategory.DataTextField = "Name";
            drpCategory.DataValueField = "Id";
            drpCategory.DataBind();
        }
    }
    

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-19
        • 2015-08-06
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-25
        • 2013-10-30
        相关资源
        最近更新 更多