Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create a Repeater with Mutiple HeaderTemplates 1

Status
Not open for further replies.

SGLong

Programmer
Jun 6, 2000
405
US
I'm very new to ASP.net, but I'm very experienced with queries and VB.Net. I'm trying to build a dynamic web page that shows a schedule of events. The number of events can vary by day. What I'd like is something like this:

Monday, May 28, 2007:
Opening - John Doe
Cooking - Mary Doe
Prep #1 -
Cleaning - John Smith
Tuesday, May 29, 2007:
Opening - John Doe
Cooking -
Prep #1 - John Smith
Prep #2 -
Cashier - Susan Doe
Cleaning -

I have a dataset that contains all of the work dates, tasks, and the assigned workers. How do I conditionally print another HeaderTemplate when the work date changes?

Steve

 
You could try using a nested data control. Have a search on google for "nested repeaters" or "nested gridviews" for some examples. Also, have a look at this microsoft kb article.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Mark (and others),

I've been struggling with this for the past several days and have hit a brick wall. I'm unable to get the first level of the nesting to work, so until I do I'm just using simple text for the second level. Here's what I've got so far

The VB.Net Code Behind
Code:
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim ZionData As New DataAccessClass
    Dim zlRdr As OleDbDataReader

    ' The Outer Schedule consists of just dates and general description only
    OuterSchedule = ZionData.GetDatesOnly
    Outer_Repeater.DataSource = OuterSchedule
    Outer_Repeater.DataBind()

    ' The Middle Schedule consists of each shifts information - Date, ShiftGroup identifier, shift hours and shift note
    MiddleSchedule = ZionData.GetUpcomingShifts

End Sub

The ASPX Code:
Code:
<table style="LEFT: 8px; WIDTH: 888px; POSITION: absolute; TOP: 250px" width="888" cellSpacing="0" cellPadding="0" border="1">
        <asp:repeater id="Outer_Repeater" runat="server">
					<HeaderTemplate>
	<tr>
	<td style="WIDTH: 888px" colspan="4" align=left >
	    [COLOR=red]<%# DataBinder.Eval(Container.DataItem, "NiceDate") %> [/color]
	</td>
	</tr>
</HeaderTemplate>
<ItemTemplate>
	<tr>
	<td style="WIDTH: 12px" >&nbsp;	</td>
	<td>
        	[COLOR=blue]<!--%# Container.DataItem("NiceDate") %-->[/color]
		Column B
	</td>
	<td>
		Column C
	</td>
	<td>
		Column D
	</td>
	</tr>
</ItemTemplate>
<FooterTemplate>
	<tr>
	    <TD style="WIDTH: 888px" colspan="4">This is the Outer Group Footer</TD>
	</tr>
</FooterTemplate>
</asp:repeater></table>

When I preview the page in the browser, the line in red is completely missing. If I substitute straight text for that line it shows up.

I know it's not a data issue because if I uncomment the blue line the desired information shows up - it's just at the wrong level of the table.

What am I doing wrong?

Steve
 
yes, that's correct. the header and footer templates do not interact with the "row level" data (your records). Only [Alternate]ItemTemplates do.

how is your data structured within the dataset (tables, relations, PK, FK)?

if you have a table of dates, and a table of tasks, and a relation between the 2 you can use [tt]DataRowView.CreateChildView()[/tt].

here is a simple sudo code example. my syntax may be off. I'm writing from scratch
Code:
<asp:Repeater id="myRepeater" OnDataRowBound="myRepeater_DataRowBound">
  <HeaderTemplate> Dates: <ol> </HeaderTemplate>
  <ItemTemplate>
      <li>
          <%# Eval("Date", "MM-dd-yy") %>
          <asp:Repeater id="mySubRepeater">
             <HeaderTemplate> <ol> </HeaderTemplate>
             <ItemTemplate>
                 <li>
                    <%#Eval("Task") %> - <%#Eval("Name") %>
                 </li>
             </ItemTemplate>
             <FooterTemplate> </ol> </FooterTemplate>
          </asp:Repeater>
      </li>
  </ItemTemplate>
  <FooterTemplate> </ol> </FooterTemplate>
</asp:Repeater>

protected sub Page_Load(...)
   if not page.ispostback then
       Me.myRepeater.DataSource = //get dataset
       Me.myRepeater.DataBind()
   end if
end sub

protected sub myRepeater_DataRowBound(...)
{
    if e.item.itemIndex > -1 then
         Dim DataRowView row as DirectCast(e.item.DataItem, DataRowView)
         Dim repeater as Repeater = DirectCast(e.item.FindControl("mySubRepeater"), Repeater)
         repeater.DataSource = row.CreateChildView("[DateToTaskRelation")
         repeater.DataBind() 
    end if
}
the datasource would look like this
Code:
Table 1 "Dates"
-------------
Date DateTime

Table 2 "Tasks"
-------------
Date DateTime
Task string
Name string

Relation "DateToTaskRelation"
-------------
Dates.Date to Tasks.Date
the output would then look like
[ol]
[li]Monday, May 28, 2007:[ol]
[li]Opening - John Doe[/li]
[li]Cooking - Mary Doe[/li]
[li]Prep #1 - [/li]
[li]Cleaning - John Smith[/li][/ol][/li]
[li]Tuesday, May 29, 2007:[ol]
[li]Opening - John Doe[/li]
[li]Cooking - [/li]
[li]Prep #1 - John Smith[/li]
[li]Prep #2 -[/li]
[li]Cashier - Susan Doe[/li]
[li]Cleaning - [/li][/ol][/li]
[/ol]

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason, that looks like it should solve my problem.

Steve
 
Jason,

I'm back again. I've been struggling with this for the past several evenings and I'm still not getting the desired results. Maybe since my illustration only referenced the first / outer layer of the table I might have mislead you. Here's what I'm working with:

Code:
Table 1: DatesOnly
	Work_Date	NiceDate
	06/11/2007	Monday, June 11, 2007
	06/12/2007	Tuesday, June 12, 2007
	06/13/2007	Wednesday, June 13, 2007

Table 2: Shifts
	ShiftKey	ShiftStart	ShiftEnd
	1		7:00 AM		11:00 AM
	2		11:00 AM	 3:00 PM
	3		3:00 PM		 7:00 PM
	4		9:00 AM		 3:00 PM
	5		3:00 PM	 	 9:00 PM

Table 3: Tasks
	TaskKey	TaskName
	1	Opening
	2	Cook #1
	3	Cook #2
	4	Prep #1
	5	Prep #2
	6	Cashier
	7	Cleaning

Table 4: Assignments
	Date		ShiftKey	TaskKey		Employee
	06/11/2007	1		1		John Doe
	06/11/2007	1		2		Mary Doe
	06/11/2007	1		4
	06/11/2007	1		6		John Smith
	06/11/2007	2		2		Bill Jones
	06/11/2007	2		3		Sam Green
	06/11/2007	2		4		Eve Ning
	06/11/2007	2		5
	06/11/2007	2		6		Ben Franklin
	06/11/2007	2		7		Mr. Clean

This is the output I'm trying to get to display in a dynamic table:
Code:
Monday, June 11, 2007 - 7:00 AM-11:00 AM
   Opening - John Doe
   Cook #1 - Mary Doe
   Prep #1 - 
   Cashier - John Smith

Monday, June 11, 2007 - 11:00 AM-3:00 PM
   Cook #1 - Bill Jones
   Cook #3 - Sam Green
   Prep #1 - Eve Ning
   Prep #2 - 
   Cashier - Ben Franklin
   Cleaning - Mr. Clean

The way I have it figured I need a pair of nested repeaters, but I'm stumped on how to code them.

Any help will be deeply appreciated.

Steve
 
the first challange I see is your data is not normalized so simple binding techniques will not work.

You also don't need WorkingDate and NiceDate. You can use the string.Format, or Eval("field", "format") to format numbers, currency, dates, times, strings.
so 06/11/2007 = Monday, June 11, 2007 with Eval("MyDate", "MMM dd, yyyy"). I can't think of the weekday key off the top of my head, but I know it exists. The same goes for time Eval("MyDateTime", "hh(or HH for 24hr):mm:ss a").

you want your data structure to look more like this:
Code:
Shift
----------
ShiftId    int
ShiftStart DateTime
ShiftEnd   DateTime

Assignment
----------
Task      string
Employee  string
ShiftId   int

Relation between Shift.ShiftId and Assignment.ShiftId
to do this you have a few options.
[ol]
[*]Change your DB queries to pull the data as proposed above[/*]
[*]create a routine to modify (or return a new )DataSet structure to matched the propose schema[/*]
[*]keep your existing DataSet schema and build the output by hand using HTML and web controls.[/*]
[/ol]
Personally I don't think option 3 is valid as it will be a nightmare to maintain when you need to modify the output.

If this is the only place where this dataset is used and you have control of the data access I would opt for option 1 and modify the data retrival query.

If you don't have access to modify the DAL then option 2 is your best/only option. There are a number of ways to do this and depending on how your system is structured will determine how to go about this.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top