Repeater ITemplate all in one table

M

Matheson

I'm currently using classes implementing the ITemplate interface to
format the display of data that is bound to databound objects. I
started out using the DataList, but soon found that the datalist
performs some initial formatting for you. Specifically, it
automatically puts any bound data into a table row and table cell for
you (<tr><td>[my bound data column1]</td></tr>).

That's great, but what if you want to add another column and have it
align with the rest of the data? (<tr><td>[my bound data
column1]</td><td>[my bound data column2]</td></tr>)

So I'm trying to use the Repeater instead. It has no automatic
formatting. The end result I'm looking for is to be able to
dynamically decide which ITemplate to load for formatting the data and
then have all that data presented in one html table. I've seen
examples that accomplish this by using the ITemplate to manipulate a
string of HTML, but this seems limited. How would you then be able to
bind events to controls you wanted to add to each row, like
LinkButtons and such?

The ITemplate I am currently working with is listed below. If anybody
sees anything silly or can point me in a different direction, I would
really appreciate it. Thanks.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace sane2
{
public class IssuesTemplate : ITemplate
{
ListItemType templateType;

int count=0;
Table thisTable = new Table();

public void InstantiateIn(System.Web.UI.Control container){
thisTable.BorderWidth = 4;
thisTable.GridLines = GridLines.Both;
TableRow TR;
TableCell TD;

Label myLabel = new Label();

switch(templateType){
case ListItemType.Header:
TR = new TableRow();
TD = new TableCell();
TD.Text = "Header Text";
TR.Cells.Add(TD);
thisTable.Rows.Add(TR);
break;

case ListItemType.Item:
myLabel = new Label();
myLabel.Text = count.ToString();
myLabel.DataBinding += new EventHandler(myLabel_DataBinding);

TR = new TableRow();
TD = new TableCell();
TD.Controls.Add(myLabel);
TR.Cells.Add(TD);
thisTable.Rows.Add(TR);
break;

case ListItemType.Footer:
Label myLabel2 = new Label();
myLabel2.Text = "Footer Text";

TR = new TableRow();
TD = new TableCell();
TD.Controls.Add(myLabel2);
TR.Cells.Add(TD);
thisTable.Rows.Add(TR);

break;
}
container.Controls.Add(thisTable);
count++;
//container.Controls.Add(myLabel);
}

private void myLabel_DataBinding(object sender, System.EventArgs e)
{
Label myControl = (Label) sender;
RepeaterItem container = (RepeaterItem) myControl.NamingContainer;


myControl.Text = "<br>[" + DataBinder.Eval(container.DataItem,
"IssueID") + "]";
}

public IssuesTemplate(ListItemType type)
{
templateType = type;
}
}
}
 
M

MSFT

Hi Matheson,

How did you use the class IssuesTemplate in a DataList/Repeater control,
set it to ItemTemplate with code? To bind events to controls, I have seen
you bound databinding for the label control, is this not what you what?

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Matheson

This is in the pageLoad event (myDV is a populated Dataiew):

Repeater myR = new Repeater();
myR.DataSource = myDV;
myR.HeaderTemplate = new IssuesTemplate(ListItemType.Header);
myR.ItemTemplate = new IssuesTemplate(ListItemType.Item);
myR.FooterTemplate = new IssuesTemplate(ListItemType.Footer);
myR.DataBind();
Page.Controls.Add(myR);
 
M

MSFT

Hi Matheson,

To bind events to controls in the Repeater, I think you may add some code
in its ItemDataBound event, In this method, you can get each control in the
particualr row by:

e.Item.FindControl("ControlName")

And then use Addhandler to bind the control's event to any method you like.


Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top