首頁>Program>source

大家好,我對下拉列表有疑問.我正在將下拉列表与資料源一起使用.如何获得所選的值?

// 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();
    }
最新回復
  • 5月前
    1 #

    您可以使用 List, Dictionary, Enum, DataSet DataTable以不同的方式繫結DropDownList .
    主要,在繫結下拉列表的資料源時,您必须考虑三件事。

    DataSource-資料集或資料表或您的資料源的名稱

    DataValueField-這些欄位將被隱藏

    DataTextField-這些欄位將顯示在下拉選單中.

    您可以使用以下代碼將下拉列表作為 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..............
    }
    

  • 5月前
    2 #

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

  • 5月前
    3 #

    請參阅此鏈接上的示例.可能会對您有帮助。

    http://msdn.microsoft.com/zh-cn/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);
      }
    

  • 5月前
    4 #

    這取決於您如何設置下拉選單的預設值.使用選定的值,但是您必须設置選定的值.例如,我使用表/列表的名稱和ID欄位填充資料源.我將選定的值設置為id欄位,並將顯示設置為名稱.選擇後,我將获得id欄位.我用它来搜尋關係表並找到實體/記錄。

  • python:子集2D numpy陣列
  • jQuery Focus在Firefox上失败