我的WebPage结构如下所示:
<asp:Repeter>
<asp:Repeter>
<asp:Repeter>
<asp:DataList>
<asp:Image />
</asp:DataList>
</asp:Repeter>
</asp:Repeter>
</asp:Repeter>
在我的Webform的输出中,我获取了DataList控件中的所有图像。但是我想要显示每组的第一张图像。
这是我的代码:
的.aspx:
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="imgMinimizeCategory" runat="server" ImageUrl="~/Images/Minimize.jpg" />
</td>
<td>
<asp:Label ID="lblCategory" runat="server" Text='<%# Eval("ProductName")%>' />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Repeater ID="ChildRepeater" runat="server" DataSource='<%# Eval("Children") %>'>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="imgMinimizeGroup" runat="server" ImageUrl="~/Images/Minimize.jpg" />
</td>
<td>
<asp:Label ID="lblGroup" runat="server" Text='<%# Eval("ProductName")%>' />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Repeater ID="ChildrenRepeater" runat="server" DataSource='<%# Eval("Children") %>'>
<ItemTemplate>
<asp:DataList ID="dlProductImages" runat="server" DataSource='<%#Eval("Images") %>' DataKeyField="ImageId"
RepeatColumns="6">
<ItemTemplate>
<asp:Image ID="imageProductImage" runat="server" Height="180px" ImageUrl='<%# Eval("ImageUrl") %>' Width="180px" />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MenuItem SelectedMenuItemInMaster = Session["SelectedMenuItem"] as MenuItem;
ParentRepeater.DataSource = GetProducts(SelectedMenuItemInMaster.Value);
ParentRepeater.DataBind();
}
}
private List<Product> GetProducts(string selectedValue)
{
List<Product> ProductsOfSelectedMenuItem = new List<Product>();
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spGetMenuData", con);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["ParentID"].ToString().Trim() == selectedValue)
{
Product item = new Product();
item.ProductName = dr["ProductName"].ToString();
item.ProductId = (int)dr["ProductId"];
item.ParentId = (int)dr["ParentId"];
foreach (DataRow drChild in ds.Tables[0].Rows)
{
if (drChild["ParentID"].ToString() == dr["ProductId"].ToString())
{
GetChildItems(ds, drChild, item);
}
}
ProductsOfSelectedMenuItem.Add(item);
}
}
SqlDataAdapter daImages = new SqlDataAdapter("Select * from Images", con);
DataSet dsImages = new DataSet();
daImages.Fill(dsImages);
foreach (Product p in ProductsOfSelectedMenuItem)
{
if (p.Children != null)
{
foreach (Product child in p.Children)
{
foreach (Product c in child.Children)
{
foreach (DataRow drImages in dsImages.Tables[0].Rows)
{
if (c.ProductId == (int)drImages["ProductId"])
{
if (c.Images == null)
{
c.Images = new List<Image>();
}
c.Images.Add(new Image() { ImageId = (int)drImages["ImageId"], ImageUrl = drImages["ImageUrl"].ToString(), ProductId = (int)drImages["ProductId"] });
}
}
}
}
}
}
return ProductsOfSelectedMenuItem;
}
private static void GetChildItems(DataSet ds, DataRow dr, Product item)
{
Product childItem = new Product();
childItem.ProductName = dr["ProductName"].ToString();
childItem.ProductId = (int)dr["ProductId"];
childItem.ParentId = (int)dr["ParentId"];
foreach (DataRow drChild in ds.Tables[0].Rows)
{
if (drChild["ParentID"].ToString() == dr["ProductId"].ToString())
{
GetChildItems(ds, drChild, childItem);
}
}
if (item.Children == null)
{
item.Children = new List<Product>();
}
item.Children.Add(childItem);
}
我知道,您正在考虑这是一个非常愚蠢的问题,因为我正在将所有图像添加到集合中。但是让我解释一下。我将在下一个编程步骤中使用所有图像。所以,我不想要一个没有图像的集合。简而言之,我想要集合中的所有图像。当我在DataSource='<%# Eval("Images")%>'
标记中声明DataList
时,我想在此处查询该集合。但我不知道如何.....
更新
答案 0 :(得分:2)
您可以在DataSource属性中引用的代码后面实现一个方法,并让该方法根据您的需要返回图像的子集。
DataSource='<%# MyFilteredImageCollection(Eval("Images")) %>'