473,756 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataList and ItemTemplate

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?

Nov 19 '05 #1
6 9594
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.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
Label lbl = (System.Web.UI. WebControls.Hyp erLink)
e.Item.FindCont rol("lblCategor y");
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?

Nov 19 '05 #2
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?

Nov 19 '05 #3
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?

Nov 19 '05 #4
Following code is not tested:

<asp:DataList id="CategoryLis t" runat="server" RepeatDirection =Vertical>
<ItemTemplate >
Category: <%# DataBinder.Eval (Container.Data Item,
"Category_Field _Name") %><br>
<asp:DataList ID="ItemList" Runat=server
RepeatDirection =Vertical>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,
"Item_Field_Nam e") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
In CategoryList_It emDataBound

if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
// 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(Catego ry);
DataList itemList = (DataList)e.Ite m.FindControl(" ItemList");
itemList.DataSo urce = itemTable;
itemList.DataBi nd();
}

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?

Nov 19 '05 #5
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:

<asp:DataList id="CategoryLis t" runat="server" RepeatDirection =Vertical>
<ItemTemplate >
Category: <%# DataBinder.Eval (Container.Data Item,
"Category_Field _Name") %><br>
<asp:DataList ID="ItemList" Runat=server
RepeatDirection =Vertical>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,
"Item_Field_Nam e") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
In CategoryList_It emDataBound

if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
// 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(Catego ry);
DataList itemList = (DataList)e.Ite m.FindControl(" ItemList");
itemList.DataSo urce = itemTable;
itemList.DataBi nd();
}

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?
>

Nov 19 '05 #6
In CategoryList_It emDataBound

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem Then
' Since you use DataReader as datasource, using
' System.Data.Com mon.DbDataRecor d
Dim record As System.Data.Com mon.DbDataRecor d =
CType(e.Item.Da taItem, System.Data.Com mon.DbDataRecor d)
Dim strCategory As String = record
..Item("Categor y_Field_Name"). ToString
Dim itemTable As DataTable = GetItems("Selec t Item From tabl_name
Where Category = '" + strCategory + "'")
Dim itemList As Datalist = CType(e.Item.Fi ndControl("Item List"),
DataList)
itemList.DataSo urce = itemTable
itemList.DataBi nd()
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:

<asp:DataList id="CategoryLis t" runat="server" RepeatDirection =Vertical>
<ItemTemplate >
Category: <%# DataBinder.Eval (Container.Data Item,
"Category_Field _Name") %><br>
<asp:DataList ID="ItemList" Runat=server
RepeatDirection =Vertical>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,
"Item_Field_Nam e") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
In CategoryList_It emDataBound

if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
// 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(Catego ry);
DataList itemList = (DataList)e.Ite m.FindControl(" ItemList");
itemList.DataSo urce = itemTable;
itemList.DataBi nd();
}

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?
> >

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1768
by: pete K | last post by:
Is it possible in asp.net to have a datalist in the itemtemplate of another datalist? For example: <asp:datalist id="MyList" runat="server"> <ItemTemplate> <table border="0" width="300"> <tr> <td><%# DataBinder.Eval(Container.DataItem, "title") %></td>
0
4948
by: Barry | last post by:
Hi, I have a Datalist inside the ItemTemplate of another DataList. When I drilldown, I'm not getting any data in either the itemtemplate or headertemplate of the second DataList. I've confirmed that the datasource is being populated properly, however the itemdatabound event is not being fired. Here's some code: <asp:datalist id="BatchList" runat="server" repeatdirection="Vertical" headerstyle-cssclass="CategoryHeader"...
0
3115
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
4
3521
by: Roger R. Smith | last post by:
Anyway I am having a problem with Web Service and DataList in ASP.NET. so the best way to explain it would be with the MSPetShop 3.0 Application. I submitted an order using the Forms on http://localhost/MSPetShop. then I created my own ASp.NET Project and I have a form on which I have a DataList, b/c I want to display a bunch of items. BUt for this example I did the following: localhost.WebServices webservices = new...
1
10930
by: bill yeager | last post by:
I have a datagrid control within a datalist control. When I try and do a "Find" on the control, the object comes back with nothing and then my pgm crashes. I am 100% sure that my datagird inside my datalist has an id of "Datagrid3". dgChild comes back with "Nothing" while debugging. Here is my code in the itemdatabound event for the datalist: <code>
4
3096
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a label, a radiobuttonlist, etc. I'm driving myself insane trying to figure out how to get
2
325
by: IGotYourDotNet | last post by:
Can anyone tell me how to find a table within a datalist? I can find the labels and textboxes in the dl and change the formatting based on the data, but I can't find the table in the DL. Can anyone point me to a code sniippet or something on finding a table in the datalist?
3
10303
by: Mirek Endys | last post by:
I have DataList as part of DataList item. DataList in DataList. The parent DataList working well including Edit command, that shows Edit template and correctly bind the data into edit template (where is the child DataList).... But in case I want to make Edit in this child DataList it is not working... No edit template showed... :( this is a code that i use for the child DataList... Edit command // this is for child DataList...
3
1604
by: Jon Paal | last post by:
I have a datalist bound to an arraylist holding 3 chart controls. I want to display the charts in the datalist : .... dtlcharts.Datasource = arrCharts dtlcharts.Databind() .... <asp:datalist id="dtlcharts" runat="server" >
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.