首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DropdownList DataSource

DropdownList DataSource
EN

Stack Overflow用户
提问于 2013-01-01 03:06:00
回答 4查看 110.1K关注 0票数 8

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

代码语言:javascript
复制
// 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();
    } 
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-01-01 03:59:10

您可以使用List, Dictionary, Enum, DataSet DataTable以不同的方式绑定DropDownList。

在绑定下拉菜单的数据源时,你必须考虑三件事。

  1. DataSource -数据集或数据表或您的数据表的名称-这些字段将是hidden
  2. DataTextField -这些字段将显示在下拉列表中。

您可以使用以下代码将下拉列表作为datatable绑定到数据源

代码语言:javascript
复制
  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时进行处理,那么你必须改变事件,你可以使用SelectedIndexChanged AutoPostBack="true"来编写你的代码。

代码语言:javascript
复制
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string strQUIZ_ID=DropDownList1.SelectedValue;
    string strQUIZ_Name=DropDownList1.SelectedItem.Text;
    // Your code..............
}
票数 32
EN

Stack Overflow用户

发布于 2013-11-13 21:28:32

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        drpCategory.DataSource = CategoryHelper.Categories;
        drpCategory.DataTextField = "Name";
        drpCategory.DataValueField = "Id";
        drpCategory.DataBind();
    }
}
票数 4
EN

Stack Overflow用户

发布于 2013-01-01 11:19:03

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

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

代码语言:javascript
复制
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);

  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14105265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档