2014-07-18 2 views

답변

1
<asp:DropDownList ID="DropDownList1" runat="server" 
         DataSource = '<%# Status %>' 
         DataTextField = "status"> 
         </asp:DropDownList> 

protected DataTable Status 
    { 
     get 
     { 
      DataTable dt = new DataTable(); 

      using (con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
      { 
       string fetch_qry = @"select status from table"; 

       con.Open(); 
       using (SqlDataAdapter adpt = new SqlDataAdapter(fetch_qry, con)) 
       { 

        adpt.Fill(dt);      

       } 
      } 

       return dt; 
     } 
    } 

중요 사항 :

protected void Page_Load(object sender, EventArgs e) 
    { 
     DropDownList1.DataBind(); 
    } 

this.DataBind();도 작동하지만 페이지의 모든 컨트롤을 바인딩 : 이것은 단지 다음과 같이 데이터 바인딩 함수가 호출되는 경우 작동합니다. 구체적으로하는 것이 좋습니다. 다음과 같이

는 다른 방법으로 데이터 소스 및 DataTextField 대신 디자인 페이지의 코드 페이지를 선언 할 수 있습니다 :

드롭 다운 목록이 그리드 뷰 내부에 존재하는 경우 경우
protected void Page_Load(object sender, EventArgs e) 
    { 
     DropDownList1.DataSource = Status; 
     DropDownList1.DataTextField = "status"; 
     DropDownList1.DataBind(); 
    } 

; gridview 수준의 DataBind 함수는 데이터 표에있는 드롭 다운 목록의 데이터를 바인딩합니다. GridView1.DataBind();

관련 문제