2009-09-23 199 views
0

在我的應用程序中,我想創建一個動態數據列表並綁定數據庫值。請幫我 這是我的代碼如何動態創建數據列表

protected void btn_Click(object sender, EventArgs e) 
{ 
    DataList datlst = new DataList(); 
    da = new SqlDataAdapter("select emname from emp", con); 
    ds = new DataSet(); 
    da.Fill(ds, "emp"); 
    datlst.DataSource = ds.Tables[0]; 
    datlst.DataBind(); 
    //this.Controls.Add(datlst); 
    form1.Controls.Add(datlst); 
} 

回答

1

在代碼隱藏文件寫

DtaList dl = new DataList(); 
dl.DataSource = //Data from DB 
dl.DataBind(); 
+0

其他的事情要牢記: 1.將DataList控件的ID屬性如dl.ID =「myDataList」 ; 2.將DataList添加到頁面/控件例如使用this.Controls.Add(dl); 3.如果查詢不是動態的,則將DataList綁定到IsPostback的測試中。 – 2009-09-23 10:12:11

+0

謝謝你的回覆,但它沒有得到請重新檢查一次我發送總代碼結帳謝謝你 – 2009-09-23 10:15:57

+0

mr.msi你請幫助關於動態數據 – 2009-09-23 10:46:07