Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Nested ListView

Reply
Thread Tools

Nested ListView

 
 
Harlan Messinger
Guest
Posts: n/a
 
      03-29-2010
I've got an Item table and a Category table. The Category table has
columns categoryId and categoryName. The Item table has a categoryId
that is a foreign key into Category, and a column called typeId.

I've created a DataContext for my database. It generally works as expected.

I want to display the items grouped by category, with group headings. So
I created a LinqDataSource based on my data context to provide the
Category data, and created a ListView that uses it as a source and it
displays my category names.

In the ItemTemplate, under the category name, I have

<asp:ListView ID="listviewItem" runat="server"
DataSource='<%#Eval("Items") %>'>

This works too. I have configured this inner ListView to list data from
the items, and they care correctly listed under the appropriate categories.

HOWEVER: what do I do if I want to list only the Items having typeId=3?
 
Reply With Quote
 
 
 
 
Andy O'Neill
Guest
Posts: n/a
 
      03-29-2010

"Harlan Messinger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've got an Item table and a Category table. The Category table has
> columns categoryId and categoryName. The Item table has a categoryId that
> is a foreign key into Category, and a column called typeId.
>
> I've created a DataContext for my database. It generally works as
> expected.
>
> I want to display the items grouped by category, with group headings. So I
> created a LinqDataSource based on my data context to provide the Category
> data, and created a ListView that uses it as a source and it displays my
> category names.
>
> In the ItemTemplate, under the category name, I have
>
> <asp:ListView ID="listviewItem" runat="server"
> DataSource='<%#Eval("Items") %>'>
>
> This works too. I have configured this inner ListView to list data from
> the items, and they care correctly listed under the appropriate
> categories.
>
> HOWEVER: what do I do if I want to list only the Items having typeId=3?


One interesting aspect about linq is that it doesn't generate any sql until
you ask it to iterate the result.
What's that mean?
That allows you to use something called the pipeline filter pattern.
You can take one linq thingummy and add a criteria onto it.
Using that pattern might be overkill in this instance but would allow you to
eaily cope with further requests to refine the data.
It's a great pattern for user configurable filtering on screens or reports.

 
Reply With Quote
 
 
 
 
Harlan Messinger
Guest
Posts: n/a
 
      03-29-2010
Harlan Messinger wrote:
> I've got an Item table and a Category table. The Category table has
> columns categoryId and categoryName. The Item table has a categoryId
> that is a foreign key into Category, and a column called typeId.
>
> I've created a DataContext for my database. It generally works as expected.
>
> I want to display the items grouped by category, with group headings. So
> I created a LinqDataSource based on my data context to provide the
> Category data, and created a ListView that uses it as a source and it
> displays my category names.
>
> In the ItemTemplate, under the category name, I have
>
> <asp:ListView ID="listviewItem" runat="server"
> DataSource='<%#Eval("Items") %>'>
>
> This works too. I have configured this inner ListView to list data from
> the items, and they care correctly listed under the appropriate categories.
>
> HOWEVER: what do I do if I want to list only the Items having typeId=3?


After three hours of horsing around I figured it out. First:

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="[the namespace containing the context class]" %>

Then:

<asp:ListView
ID="listviewItem"
runat="server"
DataSource='<%# ((IEnumerable<Item>) Eval("Items")).Where(item =>
item.TypeId == 3) %>'>

(I had included the Items property among the properties selected for
inclusion in the outer Category ListView.)
 
Reply With Quote
 
Harlan Messinger
Guest
Posts: n/a
 
      03-29-2010
Andy O'Neill wrote:
>
> "Harlan Messinger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I've got an Item table and a Category table. The Category table has
>> columns categoryId and categoryName. The Item table has a categoryId
>> that is a foreign key into Category, and a column called typeId.
>>
>> I've created a DataContext for my database. It generally works as
>> expected.
>>
>> I want to display the items grouped by category, with group headings.
>> So I created a LinqDataSource based on my data context to provide the
>> Category data, and created a ListView that uses it as a source and it
>> displays my category names.
>>
>> In the ItemTemplate, under the category name, I have
>>
>> <asp:ListView ID="listviewItem" runat="server"
>> DataSource='<%#Eval("Items") %>'>
>>
>> This works too. I have configured this inner ListView to list data
>> from the items, and they care correctly listed under the appropriate
>> categories.
>>
>> HOWEVER: what do I do if I want to list only the Items having typeId=3?

>
> One interesting aspect about linq is that it doesn't generate any sql
> until you ask it to iterate the result.
> What's that mean?
> That allows you to use something called the pipeline filter pattern.
> You can take one linq thingummy and add a criteria onto it.
> Using that pattern might be overkill in this instance but would allow
> you to eaily cope with further requests to refine the data.
> It's a great pattern for user configurable filtering on screens or reports.
>

Before seeing your reply I sent a follow-up to my note where I show a
solution I came up with. I think it's the kind of thing you're referring
to, so thanks for the reinforcement.

However, I've come up with another puzzler: in the outer ListView, I
have categories that aren't associated with any of the items in my data,
so I don't want them to display. In SQL I'd use "select distinct
Category.categoryId, Category.categoryName from Item inner join Category
...." or "select categoryId, categoryName from Category where exists ..."
but I haven't figure out how to accomplish that with the
LinqDataSource/ListView combination. Any thoughts?
 
Reply With Quote
 
Harlan Messinger
Guest
Posts: n/a
 
      03-29-2010
Harlan Messinger wrote:
> However, I've come up with another puzzler: in the outer ListView, I
> have categories that aren't associated with any of the items in my data,
> so I don't want them to display. In SQL I'd use "select distinct
> Category.categoryId, Category.categoryName from Item inner join Category
> ..." or "select categoryId, categoryName from Category where exists ..."
> but I haven't figure out how to accomplish that with the
> LinqDataSource/ListView combination. Any thoughts?


Holy cow, I figured that one out too (after three more hours of
experimentation). You don't even need a TableName attribute in the
LinqDataSource tag. Create a handler for the Selecting event:

protected void linqsrcCategory_Selecting(object sender,
LinqDataSourceSelectEventArgs e)
{
AuctionData context = new AuctionData();
e.Result = (from item in context.Items
where item.TypeId == 3
select item.Category)
.Distinct()
.OrderBy(category => category.CategoryId)
.ToList();
}
 
Reply With Quote
 
Andy O'Neill
Guest
Posts: n/a
 
      03-29-2010

"Harlan Messinger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Andy O'Neill wrote:

<<>>
> Before seeing your reply I sent a follow-up to my note where I show a
> solution I came up with. I think it's the kind of thing you're referring
> to, so thanks for the reinforcement.


That sort of solution will of course only cope with the one parameter.
The pipeline filter thing allows a list of different filters to be applied.
May be of no interest to you right now, but when they come back and want you
to filter on any combination of 5 different options - that's the thing to
look into.

 
Reply With Quote
 
Harlan Messinger
Guest
Posts: n/a
 
      03-29-2010
Andy O'Neill wrote:
>
> "Harlan Messinger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Andy O'Neill wrote:

> <<>>
>> Before seeing your reply I sent a follow-up to my note where I show a
>> solution I came up with. I think it's the kind of thing you're
>> referring to, so thanks for the reinforcement.

>
> That sort of solution will of course only cope with the one parameter.
> The pipeline filter thing allows a list of different filters to be applied.
> May be of no interest to you right now, but when they come back and want
> you to filter on any combination of 5 different options - that's the
> thing to look into.


Google didn't turn up anything substantive on "pipeline filter" in
conjunction with LINQ. Is that the usual name for it?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp.net ListView control - Configure ListView option missing? . ASP .Net 0 07-24-2010 12:20 AM
Nested ListView lazy-loaded Kodiak ASP .Net 0 05-02-2009 02:24 PM
edit all items in ListView, even nested ListViews Ryan Liu ASP .Net 1 01-21-2009 08:15 AM
FindControl to reference nested controls in gridview/listview Martin ASP .Net 4 04-10-2008 10:08 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments