当前位置:首页 > DataList嵌套DataList(3.使用DataRelation实现)

DataList嵌套DataList(3.使用DataRelation实现)

点击次数:1138  更新日期:2010-12-30
\n

aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”DataListNesting.aspx.cs” Inherits=”DataListNesting” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>DataListNesting</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataList ID=”DataList1″ runat=”server”>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(“OrderID”) %>’></asp:Label>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(“CustomerID”) %>’></asp:Label>
<asp:DataList ID=”DataList2″ runat=”server” DataSource=’<%# (Container.DataItem as System.Data.DataRowView).Row.GetChildRows(“myRelation”) %>’>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["ProductID"] %>’></asp:Label>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["UnitPrice"] %>’></asp:Label>
<asp:Label ID=”Label3″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["Quantity"] %>’></asp:Label>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”DataListNesting.aspx.cs” Inherits=”DataListNesting” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>DataListNesting</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataList ID=”DataList1″ runat=”server”>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(“OrderID”) %>’></asp:Label>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(“CustomerID”) %>’></asp:Label>
<asp:DataList ID=”DataList2″ runat=”server” DataSource=’<%# (Container.DataItem as System.Data.DataRowView).Row.GetChildRows(“myRelation”) %>’>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["ProductID"] %>’></asp:Label>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["UnitPrice"] %>’></asp:Label>
<asp:Label ID=”Label3″ runat=”server” Text=’<%# (Container.DataItem as System.Data.DataRow)["Quantity"] %>’></asp:Label>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DataListNesting : System.Web.UI.Page
{
private void BindList()
{
SqlConnection cn = new SqlConnection(@”server=.\\sqlexpress;uid=sa;pwd=;database=northwind;”);
SqlDataAdapter da = new SqlDataAdapter(“select OrderID, CustomerID from Orders; select OrderID, ProductID, UnitPrice, Quantity from [Order Details]“, cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
ds.Tables[0].TableName = “Orders”;
ds.Tables[1].TableName = “OrderDetails”;

DataRelation ordersToOrderDetails = ds.Relations.Add(“myRelation”, ds.Tables["Orders"].Columns["OrderID"], ds.Tables["OrderDetails"].Columns["OrderID"]);

DataList1.DataSource = ds.Tables["Orders"];
DataList1.DataBind();
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindList();
}
}

//protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
//{
// DataList DataList2;
// if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
// {
// DataList2 = e.Item.FindControl(“DataList2″) as DataList;
// DataList2.DataSource = ((DataRowView)(e.Item.DataItem)).Row.GetChildRows(“myRelation”);
// DataList2.DataBind();
// }
//}
}


\n

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


\n

public partial class DataListNesting : System.Web.UI.Page
{
private void BindList()
{
SqlConnection cn = new SqlConnection(@”server=.\\sqlexpress;uid=sa;pwd=;database=northwind;”);
SqlDataAdapter da = new SqlDataAdapter(“select OrderID, CustomerID from Orders; select OrderID, ProductID, UnitPrice, Quantity from [Order Details]“, cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
ds.Tables[0].TableName = “Orders”;
ds.Tables[1].TableName = “OrderDetails”;


\n

DataRelation ordersToOrderDetails = ds.Relations.Add(“myRelation”, ds.Tables["Orders"].Columns["OrderID"], ds.Tables["OrderDetails"].Columns["OrderID"]);


\n

DataList1.DataSource = ds.Tables["Orders"];
DataList1.DataBind();
}


\n

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindList();
}
}


\n

//protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
//{
// DataList DataList2;
// if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
// {
// DataList2 = e.Item.FindControl(“DataList2″) as DataList;
// DataList2.DataSource = ((DataRowView)(e.Item.DataItem)).Row.GetChildRows(“myRelation”);
// DataList2.DataBind();
// }
//}
}


\n

使用DataRelation的方式仍然可以使用ItemDataBound事件,有兴趣的朋友可以参考上面的注释代码

来源:http://blog.csdn.net/amandag

\n