关于 ObjectDataSource 嵌套的问题:内层 ObjectDataSource 如何获取外层的数据?
业务逻辑中有两个函数:
getCategorys,返回List(Of CategoryItem)
getInfos,参数categoryID,返回List(Of InfoItem)
目的是将各分类下信息显示出来,于是用两个 ObjectDataSource 嵌套:
<asp:ObjectDataSource id= "categorys " ...>
<asp:Repeater ... >
<ItemTemplate>
<asp:ObjectDataSource id= "infos " ...> --------这里如何将外层的CategoryItem.DataID 传给它???
<asp:Repeater ... >
<ItemTemplate>
....
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
[解决办法]
既然了解结构,DataSource应该在循环外边就预先创建,放在repeater里边反复创建同样的数据集有点太浪费CPU时间了。
[解决办法]
<%@ Page Language= "C# " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
System.Data.DataRowView drv = e.Item.DataItem as System.Data.DataRowView;
SqlDataSource sds2 = e.Item.FindControl( "SqlDataSource2 ") as SqlDataSource;
sds2.SelectCommand = String.Format( "select * from [order details] where orderid={0} ", drv[ "OrderID "]);
}
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$ ConnectionStrings:NorthwindConnectionString %> "
SelectCommand= "select * from orders where orderid between 10248 and 10253 "> </asp:SqlDataSource>
</div>
<asp:Repeater ID= "Repeater1 " runat= "server " DataSourceID= "SqlDataSource1 " OnItemDataBound= "Repeater1_ItemDataBound ">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
CustomerID: <%#DataBinder.Eval(Container.DataItem, "CustomerID ") %>
/ EmployeeID: <%# DataBinder.Eval(Container.DataItem, "EmployeeID ")%>
/ OrderID: <%# DataBinder.Eval(Container.DataItem, "OrderID ")%> </td>
</tr>
<tr>
<td>
<asp:SqlDataSource ID= "SqlDataSource2 " runat= "server " ConnectionString= " <%$ ConnectionStrings:NorthwindConnectionString %> ">
</asp:SqlDataSource>
<asp:Repeater runat= "server " ID= "repOrders " DataSourceID= "SqlDataSource2 ">
<ItemTemplate>
<ul>
<li> ProductID: <%# DataBinder.Eval(Container.DataItem, "ProductID ")%> </li>
<li> UnitPrice: <%# DataBinder.Eval(Container.DataItem, "UnitPrice ")%> </li>
<li> Quantity: <%# DataBinder.Eval(Container.DataItem, "Quantity ")%> </li>
<li> Discount: <%# DataBinder.Eval(Container.DataItem, "Discount ")%> </li>
</ul>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>