Server control derived from Repeater?

  • Thread starter Thread starter Dobedani
  • Start date Start date
D

Dobedani

Dear All,

I would like to use a Repeater to show data - mostly large volumes
from a database - in a comma delimited format. VS.NET documentation
mentions this as one of the uses of the Repeater! Actually, the idea
is that users can save the output of a request as CSV file on their
desktop. For that reason, I stripped my aspx of all html and left only
the first line:
<%@ Page language="c#" Codebehind="csv.aspx.cs" etc. %>

I hope to be able to add the repeater on the fly, like in the
following:
Repeater rptData = new Repeater();
this.Controls.Add(rptData);

However, it appears impossible to edit the templates in the code. I
tried assigning strings to the HeaderTemplate and ItemTemplate, but I
got:
Cannot convert type 'string' to 'System.Web.UI.ITemplate' even when I
tried to cast my strings.

I am now thinking that to create a server control derived from
Repeater is the way forward. Am I right? How to go about it? And what
should the ItemTemplate look like? I hope to avoid databinding with
DataBinder.Eval because that will make the page terribly slow.
Moreover, the number of columns is variable (max. 13). I hope somebody
can help me. TIA

Kind regards,
Dobedani
Wageningen
The Netherlands
 
I have never tried adding columns to a repeater control dynamically,
but I have done this with a datagrid control, and I think the
technique whould be very similar.

1. You need to create a class that implments ITemplate
2. In this class, implement InstantiateIn.
InstantiateIn allows you to
a. Modify the template to contain any control you like
b. Attach a handle the databinding event.
3. Create a method that will handle the databinding event, such as
HandlingDataBinding
4. In the HandlingDataBinding method, you can add the data from your
data source to your controls.
5. For each column you need in the repeater control, add an instance
of the
class you've created above.

Tommy,
 
Back
Top