Dynamic Repeater and ItemTemplates

  • Thread starter Mr Not So Know It All
  • Start date
M

Mr Not So Know It All

i found this code to create dynamic itemtemplates on the microsoft
site. here's the url:

Creating Web Server Control Templates Programmatically
http://msdn.microsoft.com/library/d...webservercontroltemplatesprogrammatically.asp

here's the code I'm using..

public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
Button btn = new Button();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<TABLE
border=1><TR><TH>Count</TH><TH>Items</TH></TR>";
break;
case ListItemType.Item:
lc.Text = "<TR><TD>Item number: " +
itemcount.ToString() + "</TD><TD>";
lc.DataBinding += new
EventHandler(TemplateControl_DataBinding);
btn.DataBinding +=new
EventHandler(TemplateControlBTN_DataBinding);
break;
case ListItemType.AlternatingItem:
lc.Text = "<TR><TD bgcolor=lightblue>Item number: " +
itemcount.ToString() + "</TD><TD>";
lc.DataBinding += new
EventHandler(TemplateControl_DataBinding);
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
break;
}
container.Controls.Add(lc);
itemcount += 1;
}

as you can see, the code dynamically adds a literal control to the item
template. can someone please help me add other controls like a button?
i would really like to add a table control and not have to use a
literal control to add the table control.

thanks for all your help.
 
M

Mr Not So Know It All

i was able to add the table via the codebehind to the repeater
(itemtemplate). the key was to add it to the container. however, now,
the data is repeating itself in each row. can someone help me
understand how the repeater works? thx in advance.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MyTable</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tbl_Default" runat="server" />
<asp:Repeater ID="rpt_Default" runat="server" />
</div>
</form>
</body>
</html>
++++++++++++++++++++++++++++++codebehind++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MyDefault : Page
{
protected Table tbl_Default;
protected Repeater rpt_Default;

public SqlConnection conn;
public SqlCommand comm;
public SqlDataReader reader;

void Page_Load(object s, EventArgs e)
{
conn = new
SqlConnection("server=xxxx;uid=mp3Admin;pwd=xxxxxxx;database=xxx");
conn.Open();
comm = new SqlCommand("select * from _XXXX where ID<25", conn);
reader = comm.ExecuteReader();

rpt_Default.HeaderTemplate = new
MyTemplate(ListItemType.Header, tbl_Default);
rpt_Default.ItemTemplate = new MyTemplate(ListItemType.Item,
tbl_Default);
rpt_Default.AlternatingItemTemplate = new
MyTemplate(ListItemType.AlternatingItem, tbl_Default);
rpt_Default.DataSource = reader;
rpt_Default.DataBind();
}
}

// C#
public class MyTemplate : ITemplate
{
static int itemcount = 0;
ListItemType templateType;
Table myTable;
public MyTemplate(ListItemType type)
{
templateType = type;
}

public MyTemplate(ListItemType type, Table tbl)
{
templateType = type;
myTable = tbl;
}

public void InstantiateIn(System.Web.UI.Control container)
{
TableRow thr = new TableRow();
TableCell thc = new TableCell();
TableRow trI = new TableRow();
TableCell tcI = new TableCell();
switch (templateType)
{
case ListItemType.Header:
myTable.BackColor = Color.Orange;
thc.Text = "Items";
thr.Cells.Add(thc);
myTable.Rows.Add(thr);
break;
case ListItemType.Item:
tcI.Text = "Item Number : " + itemcount.ToString()
tcI.DataBinding += new
EventHandler(TemplateControlTC_DataBinding);
trI.Cells.Add(tcI);
break;
case ListItemType.AlternatingItem:
tcI.DataBinding += new
EventHandler(TemplateControlTC_DataBinding);
tcI.Text = "Item Number : " + itemcount.ToString()
trI.Cells.Add(tcI);
trI.BackColor = Color.Blue;
break;
case ListItemType.Footer:
break;
}
myTable.Rows.Add(trI);
container.Controls.Add(myTable);
itemcount += 1;
}


private void TemplateControlTC_DataBinding(object sender,
System.EventArgs e)
{
TableCell tc;
tc = (TableCell)sender;
RepeaterItem container = (RepeaterItem)tc.NamingContainer;
tc.Text += DataBinder.Eval(container.DataItem, "Filename");
}

}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top