복붙노트

[SQL] 드롭 다운리스트 데이터 소스

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

해결법

  1. ==============================

    1.당신은 목록, 사전, 열거, 데이터 집합의 DataTable을 사용하여 다른 방식으로 DropDownList로 바인딩 할 수 있습니다. 주요 당신은 드롭 다운의 데이터 소스를 바인딩하면서 세 가지를 고려해야합니다.

    당신은 목록, 사전, 열거, 데이터 집합의 DataTable을 사용하여 다른 방식으로 DropDownList로 바인딩 할 수 있습니다. 주요 당신은 드롭 다운의 데이터 소스를 바인딩하면서 세 가지를 고려해야합니다.

    당신은 데이터 테이블로 데이터 소스에 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();
    

    당신이 드롭 다운리스트의 선택에 처리 할 경우, 당신은 당신이 당신의 코드를 작성의 SelectedIndexChanged 이벤트를 사용할 수 있습니다 = "true"로 AutoPostBack을 변경해야합니다.

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

    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. ==============================

    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. ==============================

    4.그것은 당신이 드롭 다운의 기본값을 설정하는 방법에 따라 달라집니다. 사용 가치를 선택,하지만 당신은 선택한 값을 설정해야합니다. 예를 들어, 나는 테이블 / 목록의 이름과 ID 필드와 데이터 소스를 채 웁니다. 나는 id 필드에 선택된 값과 이름 표시를 설정합니다. 내가 선택하면, 나는 id 필드를 얻을. 나는 관계형 테이블을 검색하고 엔티티 / 기록을 찾으려면이를 사용합니다.

    그것은 당신이 드롭 다운의 기본값을 설정하는 방법에 따라 달라집니다. 사용 가치를 선택,하지만 당신은 선택한 값을 설정해야합니다. 예를 들어, 나는 테이블 / 목록의 이름과 ID 필드와 데이터 소스를 채 웁니다. 나는 id 필드에 선택된 값과 이름 표시를 설정합니다. 내가 선택하면, 나는 id 필드를 얻을. 나는 관계형 테이블을 검색하고 엔티티 / 기록을 찾으려면이를 사용합니다.

  5. from https://stackoverflow.com/questions/14105265/dropdownlist-datasource by cc-by-sa and MIT license