如何在DataList里再套一个DataList

来源:互联网 发布:武汉家装网源码 编辑:程序博客网 时间:2024/06/08 10:33
 
字体变小 字体变大
    一、新建一个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();
     }
}

 

    第九步:编译生成,再查看即可

原创粉丝点击