Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataList and ItemTemplate

Reply
Thread Tools

DataList and ItemTemplate

 
 
=?Utf-8?B?UGF1bA==?=
Guest
Posts: n/a
 
      10-17-2005
I am trying to use a DataList and the ItemTemplate.

I am binding the Datalist to a SQL query that gives me a list of Items with
a Parent Category.

I want to loop through all the items, but only print the Parent Category
once, regardless of how many child items are in it.

So my perfect output would look something like:

Category A
Item 1
Item 2
Item 3

Category B
Item 1

Category C
Item 1
Item 2


Can I use the ItemTemplate to produce this type of output?

I know that I can easily get this type of output
Category A
Item 1

Category A
Item 2

Category A
Item 3

Category B
Item 1

Category B
Item 2

Category C
Item 1
....

It does not appear that I can embed ASP code within the ItemTemplate tags?
If I try, then it says that I have not declared the Container object, which
holds the Item and Category values.

Am I missing something easy here?

 
Reply With Quote
 
 
 
 
=?Utf-8?B?YnJpYW5zW01DU0Rd?=
Guest
Posts: n/a
 
      10-17-2005
Hello Paul,

If I were you, I'd look at the ItemDataBound event for the datalist. I do
something similar for a blog I keep. I want to list the blog entry and
any/all comments:

blogentry1
comment1
comment2
blogentry2
comment3
comment4

While it's not the exact same situation as you have since the comments are
uniquely tied to a blogentry, it's similar.

In the ItemDataBound event, you can get your categoryid and then look up any
item(s) which may belong to it. Something along these lines:
=-=-=-=-=-=-
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Label lbl = (System.Web.UI.WebControls.HyperLink)
e.Item.FindControl("lblCategory");
if (lbl != null)
{
... find any items which match this category and list them
}

}
=-=-=-=-=-
A deeper explanation of the ItemDataBound event with a sample can be found at
http://www.codeproject.com/aspnet/ItemCreated.asp. Or do a search in google:
http://www.google.com/search?hl=en&q=itemdatabound for more.

--
brians
http://www.limbertech.com


"Paul" wrote:

> I am trying to use a DataList and the ItemTemplate.
>
> I am binding the Datalist to a SQL query that gives me a list of Items with
> a Parent Category.
>
> I want to loop through all the items, but only print the Parent Category
> once, regardless of how many child items are in it.
>
> So my perfect output would look something like:
>
> Category A
> Item 1
> Item 2
> Item 3
>
> Category B
> Item 1
>
> Category C
> Item 1
> Item 2
>
>
> Can I use the ItemTemplate to produce this type of output?
>
> I know that I can easily get this type of output
> Category A
> Item 1
>
> Category A
> Item 2
>
> Category A
> Item 3
>
> Category B
> Item 1
>
> Category B
> Item 2
>
> Category C
> Item 1
> ...
>
> It does not appear that I can embed ASP code within the ItemTemplate tags?
> If I try, then it says that I have not declared the Container object, which
> holds the Item and Category values.
>
> Am I missing something easy here?
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      10-17-2005
Hi Paul,

The better way to achieve it is to use another list control (DataList,
DataGrid, or Repeater) nested in the DataList. In outer DataList, it loops
thru Categories. And in the inner list control, it binds a data source with
Items associated with a Category.

HTH

Elton Wang


"Paul" wrote:

> I am trying to use a DataList and the ItemTemplate.
>
> I am binding the Datalist to a SQL query that gives me a list of Items with
> a Parent Category.
>
> I want to loop through all the items, but only print the Parent Category
> once, regardless of how many child items are in it.
>
> So my perfect output would look something like:
>
> Category A
> Item 1
> Item 2
> Item 3
>
> Category B
> Item 1
>
> Category C
> Item 1
> Item 2
>
>
> Can I use the ItemTemplate to produce this type of output?
>
> I know that I can easily get this type of output
> Category A
> Item 1
>
> Category A
> Item 2
>
> Category A
> Item 3
>
> Category B
> Item 1
>
> Category B
> Item 2
>
> Category C
> Item 1
> ...
>
> It does not appear that I can embed ASP code within the ItemTemplate tags?
> If I try, then it says that I have not declared the Container object, which
> holds the Item and Category values.
>
> Am I missing something easy here?
>

 
Reply With Quote
 
=?Utf-8?B?UGF1bA==?=
Guest
Posts: n/a
 
      10-18-2005
Thanks for this.

Is there an code example of nested DataLists anyplace? A code example of a
nested DataList that uses a value from the outer Datalist?




"Elton W" wrote:

> Hi Paul,
>
> The better way to achieve it is to use another list control (DataList,
> DataGrid, or Repeater) nested in the DataList. In outer DataList, it loops
> thru Categories. And in the inner list control, it binds a data source with
> Items associated with a Category.
>
> HTH
>
> Elton Wang
>
>
> "Paul" wrote:
>
> > I am trying to use a DataList and the ItemTemplate.
> >
> > I am binding the Datalist to a SQL query that gives me a list of Items with
> > a Parent Category.
> >
> > I want to loop through all the items, but only print the Parent Category
> > once, regardless of how many child items are in it.
> >
> > So my perfect output would look something like:
> >
> > Category A
> > Item 1
> > Item 2
> > Item 3
> >
> > Category B
> > Item 1
> >
> > Category C
> > Item 1
> > Item 2
> >
> >
> > Can I use the ItemTemplate to produce this type of output?
> >
> > I know that I can easily get this type of output
> > Category A
> > Item 1
> >
> > Category A
> > Item 2
> >
> > Category A
> > Item 3
> >
> > Category B
> > Item 1
> >
> > Category B
> > Item 2
> >
> > Category C
> > Item 1
> > ...
> >
> > It does not appear that I can embed ASP code within the ItemTemplate tags?
> > If I try, then it says that I have not declared the Container object, which
> > holds the Item and Category values.
> >
> > Am I missing something easy here?
> >

 
Reply With Quote
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      10-18-2005
Following code is not tested:

<aspataList id="CategoryList" runat="server" RepeatDirection=Vertical>
<ItemTemplate>
Category: <%# DataBinder.Eval(Container.DataItem,
"Category_Field_Name") %><br>
<aspataList ID="ItemList" Runat=server
RepeatDirection=Vertical>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"Item_Field_Name") %>
</ItemTemplate>
</aspataList>
</ItemTemplate>
</aspataList>


In CategoryList_ItemDataBound

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// Suppose you use DataTable, or DataView, or DataSet as Data Source
DataRowView drv = (DataRowView)e.Item.DataItem;
string Category = drv["Category_Field_Name"].ToString();
DataTable itemTable = GetItems(Category);
DataList itemList = (DataList)e.Item.FindControl("ItemList");
itemList.DataSource = itemTable;
itemList.DataBind();
}

HTH

Elton Wang



"Paul" wrote:

> Thanks for this.
>
> Is there an code example of nested DataLists anyplace? A code example of a
> nested DataList that uses a value from the outer Datalist?
>
>
>
>
> "Elton W" wrote:
>
> > Hi Paul,
> >
> > The better way to achieve it is to use another list control (DataList,
> > DataGrid, or Repeater) nested in the DataList. In outer DataList, it loops
> > thru Categories. And in the inner list control, it binds a data source with
> > Items associated with a Category.
> >
> > HTH
> >
> > Elton Wang
> >
> >
> > "Paul" wrote:
> >
> > > I am trying to use a DataList and the ItemTemplate.
> > >
> > > I am binding the Datalist to a SQL query that gives me a list of Items with
> > > a Parent Category.
> > >
> > > I want to loop through all the items, but only print the Parent Category
> > > once, regardless of how many child items are in it.
> > >
> > > So my perfect output would look something like:
> > >
> > > Category A
> > > Item 1
> > > Item 2
> > > Item 3
> > >
> > > Category B
> > > Item 1
> > >
> > > Category C
> > > Item 1
> > > Item 2
> > >
> > >
> > > Can I use the ItemTemplate to produce this type of output?
> > >
> > > I know that I can easily get this type of output
> > > Category A
> > > Item 1
> > >
> > > Category A
> > > Item 2
> > >
> > > Category A
> > > Item 3
> > >
> > > Category B
> > > Item 1
> > >
> > > Category B
> > > Item 2
> > >
> > > Category C
> > > Item 1
> > > ...
> > >
> > > It does not appear that I can embed ASP code within the ItemTemplate tags?
> > > If I try, then it says that I have not declared the Container object, which
> > > holds the Item and Category values.
> > >
> > > Am I missing something easy here?
> > >

 
Reply With Quote
 
=?Utf-8?B?UGF1bA==?=
Guest
Posts: n/a
 
      10-18-2005
Thank you for this. I may be getting closer.

The problem with this is that each DataList is databinded to a SQL query
(which you do not show here). The DataSource is a DataReader object.

The inner DataList is binded to a SQL query that uses a value from the outer
DataList. (i.e. select * from items where category = x).

So I am not sure how the inner DataList could be dynamic in that sense.

Also, I use VB.Net, not C. So I am unsure what C's "In" statement equivalent
is in VB?

TIA.





"Elton W" wrote:

> Following code is not tested:
>
> <aspataList id="CategoryList" runat="server" RepeatDirection=Vertical>
> <ItemTemplate>
> Category: <%# DataBinder.Eval(Container.DataItem,
> "Category_Field_Name") %><br>
> <aspataList ID="ItemList" Runat=server
> RepeatDirection=Vertical>
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem,
> "Item_Field_Name") %>
> </ItemTemplate>
> </aspataList>
> </ItemTemplate>
> </aspataList>
>
>
> In CategoryList_ItemDataBound
>
> if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> // Suppose you use DataTable, or DataView, or DataSet as Data Source
> DataRowView drv = (DataRowView)e.Item.DataItem;
> string Category = drv["Category_Field_Name"].ToString();
> DataTable itemTable = GetItems(Category);
> DataList itemList = (DataList)e.Item.FindControl("ItemList");
> itemList.DataSource = itemTable;
> itemList.DataBind();
> }
>
> HTH
>
> Elton Wang
>
>
>
> "Paul" wrote:
>
> > Thanks for this.
> >
> > Is there an code example of nested DataLists anyplace? A code example of a
> > nested DataList that uses a value from the outer Datalist?
> >
> >
> >
> >
> > "Elton W" wrote:
> >
> > > Hi Paul,
> > >
> > > The better way to achieve it is to use another list control (DataList,
> > > DataGrid, or Repeater) nested in the DataList. In outer DataList, it loops
> > > thru Categories. And in the inner list control, it binds a data source with
> > > Items associated with a Category.
> > >
> > > HTH
> > >
> > > Elton Wang
> > >
> > >
> > > "Paul" wrote:
> > >
> > > > I am trying to use a DataList and the ItemTemplate.
> > > >
> > > > I am binding the Datalist to a SQL query that gives me a list of Items with
> > > > a Parent Category.
> > > >
> > > > I want to loop through all the items, but only print the Parent Category
> > > > once, regardless of how many child items are in it.
> > > >
> > > > So my perfect output would look something like:
> > > >
> > > > Category A
> > > > Item 1
> > > > Item 2
> > > > Item 3
> > > >
> > > > Category B
> > > > Item 1
> > > >
> > > > Category C
> > > > Item 1
> > > > Item 2
> > > >
> > > >
> > > > Can I use the ItemTemplate to produce this type of output?
> > > >
> > > > I know that I can easily get this type of output
> > > > Category A
> > > > Item 1
> > > >
> > > > Category A
> > > > Item 2
> > > >
> > > > Category A
> > > > Item 3
> > > >
> > > > Category B
> > > > Item 1
> > > >
> > > > Category B
> > > > Item 2
> > > >
> > > > Category C
> > > > Item 1
> > > > ...
> > > >
> > > > It does not appear that I can embed ASP code within the ItemTemplate tags?
> > > > If I try, then it says that I have not declared the Container object, which
> > > > holds the Item and Category values.
> > > >
> > > > Am I missing something easy here?
> > > >

 
Reply With Quote
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      10-18-2005
In CategoryList_ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
' Since you use DataReader as datasource, using
' System.Data.Common.DbDataRecord
Dim record As System.Data.Common.DbDataRecord =
CType(e.Item.DataItem, System.Data.Common.DbDataRecord)
Dim strCategory As String = record
..Item("Category_Field_Name").ToString
Dim itemTable As DataTable = GetItems("Select Item From tabl_name
Where Category = '" + strCategory + "'")
Dim itemList As Datalist = CType(e.Item.FindControl("ItemList"),
DataList)
itemList.DataSource = itemTable
itemList.DataBind()
End If


"Paul" wrote:

> Thank you for this. I may be getting closer.
>
> The problem with this is that each DataList is databinded to a SQL query
> (which you do not show here). The DataSource is a DataReader object.
>
> The inner DataList is binded to a SQL query that uses a value from the outer
> DataList. (i.e. select * from items where category = x).
>
> So I am not sure how the inner DataList could be dynamic in that sense.
>
> Also, I use VB.Net, not C. So I am unsure what C's "In" statement equivalent
> is in VB?
>
> TIA.
>
>
>
>
>
> "Elton W" wrote:
>
> > Following code is not tested:
> >
> > <aspataList id="CategoryList" runat="server" RepeatDirection=Vertical>
> > <ItemTemplate>
> > Category: <%# DataBinder.Eval(Container.DataItem,
> > "Category_Field_Name") %><br>
> > <aspataList ID="ItemList" Runat=server
> > RepeatDirection=Vertical>
> > <ItemTemplate>
> > <%# DataBinder.Eval(Container.DataItem,
> > "Item_Field_Name") %>
> > </ItemTemplate>
> > </aspataList>
> > </ItemTemplate>
> > </aspataList>
> >
> >
> > In CategoryList_ItemDataBound
> >
> > if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> > ListItemType.AlternatingItem)
> > {
> > // Suppose you use DataTable, or DataView, or DataSet as Data Source
> > DataRowView drv = (DataRowView)e.Item.DataItem;
> > string Category = drv["Category_Field_Name"].ToString();
> > DataTable itemTable = GetItems(Category);
> > DataList itemList = (DataList)e.Item.FindControl("ItemList");
> > itemList.DataSource = itemTable;
> > itemList.DataBind();
> > }
> >
> > HTH
> >
> > Elton Wang
> >
> >
> >
> > "Paul" wrote:
> >
> > > Thanks for this.
> > >
> > > Is there an code example of nested DataLists anyplace? A code example of a
> > > nested DataList that uses a value from the outer Datalist?
> > >
> > >
> > >
> > >
> > > "Elton W" wrote:
> > >
> > > > Hi Paul,
> > > >
> > > > The better way to achieve it is to use another list control (DataList,
> > > > DataGrid, or Repeater) nested in the DataList. In outer DataList, it loops
> > > > thru Categories. And in the inner list control, it binds a data source with
> > > > Items associated with a Category.
> > > >
> > > > HTH
> > > >
> > > > Elton Wang
> > > >
> > > >
> > > > "Paul" wrote:
> > > >
> > > > > I am trying to use a DataList and the ItemTemplate.
> > > > >
> > > > > I am binding the Datalist to a SQL query that gives me a list of Items with
> > > > > a Parent Category.
> > > > >
> > > > > I want to loop through all the items, but only print the Parent Category
> > > > > once, regardless of how many child items are in it.
> > > > >
> > > > > So my perfect output would look something like:
> > > > >
> > > > > Category A
> > > > > Item 1
> > > > > Item 2
> > > > > Item 3
> > > > >
> > > > > Category B
> > > > > Item 1
> > > > >
> > > > > Category C
> > > > > Item 1
> > > > > Item 2
> > > > >
> > > > >
> > > > > Can I use the ItemTemplate to produce this type of output?
> > > > >
> > > > > I know that I can easily get this type of output
> > > > > Category A
> > > > > Item 1
> > > > >
> > > > > Category A
> > > > > Item 2
> > > > >
> > > > > Category A
> > > > > Item 3
> > > > >
> > > > > Category B
> > > > > Item 1
> > > > >
> > > > > Category B
> > > > > Item 2
> > > > >
> > > > > Category C
> > > > > Item 1
> > > > > ...
> > > > >
> > > > > It does not appear that I can embed ASP code within the ItemTemplate tags?
> > > > > If I try, then it says that I have not declared the Container object, which
> > > > > holds the Item and Category values.
> > > > >
> > > > > Am I missing something easy here?
> > > > >

 
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
can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate? Bennett Haselton ASP .Net 1 09-24-2004 01:59 AM
Setting up a datalist control - Item_DataBound for a datalist in a datalist Nevyn Twyll ASP .Net 8 09-09-2004 10:13 PM
Resizing Image control in DataList ItemTemplate David W. Simmonds ASP .Net 10 01-27-2004 10:30 AM
DataList ItemTemplate and EditItemTemplate Dave ASP .Net Datagrid Control 3 01-07-2004 09:00 PM
Getting the value from ItemTemplate of DataList Vannela ASP .Net 1 12-12-2003 02:57 PM



Advertisments