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;
}
}
}
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;
}
}
}