开发者

How to show datalist in datalist itemtemplate

开发者 https://www.devze.com 2023-03-13 05:11 出处:网络
Here\'s the problem I\'m having in my ASP.NET web-form project. I have 开发者_JAVA技巧Datalist which shows all Products from Database , Each product have one or more colors .

Here's the problem I'm having in my ASP.NET web-form project.

I have 开发者_JAVA技巧Datalist which shows all Products from Database , Each product have one or more colors . At the moment i write queries like this in order to show all product and default photo of that but not colors related to that product :

var query = from p in db.Products
                        from c in db.Pics
                        where c.ProductId == p.ProductId
                        where c.IsDefault == true
                        select new { p.ProductId, p.ProductType, p.Name, p.Number, p.Package, p.Model, p.Size, p.Material, p.MantoLengh, c.PicAddress};

            DataList1.DataSource = query;
            DataList1.DataBind();

I don't know how to bind related colors to a list-control ( like datalist or other list-Controls) inside of my DataList control, here is a view of my database diagram for more information :

How to show datalist in datalist itemtemplate


I think this query would work though I apologise in advance if my syntax is slightly out, am more VB than C#...

        var query = from p in db.Products 
                    select new { p.ProductId, 
                        p.ProductType, 
                        p.Name, 
                        p.Number, 
                        p.Package, 
                        p.Model, 
                        p.Size, 
                        p.Material, 
                        p.MantoLengh, 
                        p.Pics.Where(x => x.IsDefault).Select(x => x.PicAddress).ToList(), 
                        p.Colors.Select(x => x.ColorHex).ToList() };

Basically, in the anonymouse type you select you can use lambda to fetch the one-to-many collections using Select().ToList(), and use .Where() to perform filtering.

Hope that helps.

EDIT - In response to your comment:

I chose the hex colour strings because they looked like the best bet for creating the actual colours. To cerate a colour from Hex, see this article. Where on the list do you want to add the colours?


I believe that after you have bound the results to your datalist, you should be able to do something like this in your DataList $<%#query.DataItem("Colors")%>

0

精彩评论

暂无评论...
验证码 换一张
取 消