8

こんにちは、ドロップダウンリストに問題があります。データソースでドロップダウン リストを使用しています。選択した値を取得するにはどうすればよいですか?

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

4 に答える 4

32

を使用して、さまざまな方法で DropDownList をバインドできますList, Dictionary, Enum, DataSet DataTable
主に、ドロップダウンのデータソースをバインドする際に 3 つのことを考慮する必要があります。

  1. DataSource - データセット、データテーブル、またはデータソースの名前
  2. DataValueField - これらのフィールドは非表示になります
  3. 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..............
}
于 2012-12-31T19:59:10.023 に答える
4
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        drpCategory.DataSource = CategoryHelper.Categories;
        drpCategory.DataTextField = "Name";
        drpCategory.DataValueField = "Id";
        drpCategory.DataBind();
    }
}
于 2013-11-13T13:28:31.853 に答える
2

このリンクの例を参照してください。それはあなたの助けになるかもしれません。

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

  }
于 2013-01-01T03:19:03.410 に答える
0

ドロップダウンのデフォルトをどのように設定するかによって異なります。選択した値を使用しますが、選択した値を設定する必要があります。たとえば、データソースにテーブル/リストの名前と ID フィールドを入力します。選択した値を id フィールドに設定し、表示を名前に設定します。選択すると、id フィールドが取得されます。これを使用してリレーショナル テーブルを検索し、エンティティ/レコードを見つけます。

于 2012-12-31T19:09:02.580 に答える