DATALIST裡再綁定個DATALIST

來源:互聯網
上載者:User

 
    一、建立一個aspx檔案,在裡面加入一個DataList
0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" border=0>

     第二步,編輯DataList的項模版
0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" border=0>
    第三步:在項目範本的“ItemTemplate”裡輸入“訂單號”、“客戶”,並在“客戶”後再添加一個DataList。
0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" border=0>
    第四步:編輯HTML檔案,在“訂單號”後面添加“<%# DataBinder.Eval(Container.DataItem, "訂單ID") %>”;在“客戶ID”後面添加“<% # DataBinder.Eval(Container.DataItem, "客戶ID") %>”。在“<asp:DataList id="DataList2" runat="server">”和“</asp:DataList>”中間添加“<HeaderTemplate>訂單詳細</HeaderTemplate><ItemTemplate>產品編號:<%# DataBinder.Eval(Container.DataItem, "產品ID") %>; 單價:<%# DataBinder.Eval(Container.DataItem, "單價") %>; 數量:<%# DataBinder.Eval(Container.DataItem, "數量") %>; 折扣:<%# DataBinder.Eval(Container.DataItem, "折扣") %></ItemTemplate>”
    最後的DataList部分應該是:
<asp:DataListid="DataList1"runat="server">
     <ItemTemplate>
         <P>訂單號:<%# DataBinder.Eval(Container.DataItem, "訂單ID") %><BR>
              客戶:<%# DataBinder.Eval(Container.DataItem, "客戶ID") %><BR>
              <asp:DataListid="DataList2"runat="server">
                   <HeaderTemplate>
                       &nbsp; &nbsp; 訂單詳細
                   </HeaderTemplate>
                   <ItemTemplate>
                       &nbsp; &nbsp; 產品編號:<%# DataBinder.Eval(Container.DataItem, "產品ID") %>;單價:<%# DataBinder.Eval(Container.DataItem, "單價") %>;
                       數量:<%# DataBinder.Eval(Container.DataItem, "數量") %>; 折扣:<%# DataBinder.Eval(Container.DataItem, "折扣") %>;
                   </ItemTemplate>
              </asp:DataList></P>
     </ItemTemplate>
</asp:DataList>
    第五步:編輯cs檔案。加入“using System.Data.OleDb;”的引用。然後在“private void Page_Load(object sender, System.EventArgs e)”的上面加上“protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\\Program Files\\Microsoft Office\\OFFICE11\\SAMPLES\\Northwind.mdb");”
    注意,這裡用的是Office2003內建的Northwind.mdb資料庫,如果安裝的是Office2000的話,路徑則為:C:\Program Files\Microsoft Office\OFFICE10\SAMPLES,可自行修改。
 

    第六步:在“private void Page_Load(object sender, System.EventArgs e)”裡加上
string strSql = "select top 10 訂單ID,客戶ID from 訂單 order by 訂購日期 desc";
 
conn.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
DataSet ds = new DataSet();
myAdapter.Fill(ds,"ds");
conn.Close();
 
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
 

    如此,綁定了第一個DataList。
    第七步:要綁定DataList裡的那個DataList,還要回到aspx頁面,選中DataList,在屬性項的“ItemDataBound”上雙擊,然後會自動跳到cs頁面
0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" border=0>

 

    第八步:在自動跳轉的cs頁面的“private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)”下加入以下代碼:
DataRowView drv = (DataRowView)(e.Item.DataItem);
if (drv!=null)
{
     DataList DataList2 = (DataList)e.Item.FindControl("DataList2");
     if (DataList2!=null)
     {
         string strSql = "select * from 訂單明細 where 訂單ID = "+drv["訂單ID"].ToString();
         conn.Open();
         OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
         DataSet ds = new DataSet();
         myAdapter.Fill(ds,"ds");
         conn.Close();
         DataList2.DataSource = ds.Tables[0].DefaultView;
         DataList2.DataBind();
     }
}

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/zhangzhenjie518/archive/2008/03/31/2233644.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.