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? 6 9513
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?
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?
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?
Following code is not tested:
<asp:DataList id="CategoryList" runat="server" RepeatDirection=Vertical>
<ItemTemplate>
Category: <%# DataBinder.Eval(Container.DataItem,
"Category_Field_Name") %><br>
<asp:DataList ID="ItemList" Runat=server
RepeatDirection=Vertical>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"Item_Field_Name") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
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?
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="CategoryList" runat="server" RepeatDirection=Vertical> <ItemTemplate> Category: <%# DataBinder.Eval(Container.DataItem, "Category_Field_Name") %><br> <asp:DataList ID="ItemList" Runat=server RepeatDirection=Vertical> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Item_Field_Name") %> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList>
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? >
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:
<asp:DataList id="CategoryList" runat="server" RepeatDirection=Vertical> <ItemTemplate> Category: <%# DataBinder.Eval(Container.DataItem, "Category_Field_Name") %><br> <asp:DataList ID="ItemList" Runat=server RepeatDirection=Vertical> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Item_Field_Name") %> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList>
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? > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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">...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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()
....
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |