Dynamic header output in Repeater control?

S

Steve Franks

OK this is driving me nuts - hope someone can help.

I am searching for a way to dynamically output a value within the
HeaderTemplate section of a Repeater control. Is this possible?

It is obvious how to do this in the ItemTemplate section, but how can I do
this in the Header section?

Consider the excerpt I included at the bottom of this message. In this the
DataBinder.Eval is used to dynamically output the ItemTemplate and this
works great.However in the area marked "UNIQUE TITLE HERE" I want to
dynamically write out the value of a particular variable as the title for my
table and I cannot figure out how to do this.

This is especially challenging for me because I created a .ascx file that
contains the header, and then I am using:
Repeater1.HeaderTemplate = Page.LoadTemplate("myheader.ascx");
to load it.

I am doing it this way because my header has a ton of HTML and I need to
output these tables throughout my site, so rather than keep repeating blocks
of the same HeaderTemplate section (except fot the title!) all over the
place, I've used this approach.

If I could figure out a way for the .ascx to access a variable within my
ASP.NET page, then I could just do this in my header template:

<tr><td colspan=2> <% Response.Write(varFromAspNetPage);
%> </td></tr>

Any ideas? Thanks.

<asp:Repeater id=Repeater1 runat="server">

<HeaderTemplate>

<table border=1>
<tr><td colspan=2> UNIQUE TITLE HERE </td></tr>
<tr>
<td><b>Company</b></td>
<td><b>Symbol</b></td>
</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "Name") %>
</td>
<td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %>
</td>
</tr>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>

Container.DataItem is NULL when ASP.NET is processing the header, so I do
not know how I can dynamically get a value in to the header!

Thanks,

Steve
 
J

jasonkester

Any reason you don't put the Repeater into a UserControl, rather than
trying to stuff a UserControl into your Repeater? And why does it
matter to you that your code goes into the HeaderTemplate at all? Why
not just drop any HTML to open your table structure in front of the
Repeater, then close the table outside the Repeater?

I've been using Repeaters for years, and have never come across a good
reason to bother with HeaderTemplate and FooterTemplate. What exactly
are you trying to accomplish that requires you to use them?

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top