473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Dynamic Repeater and ItemTemplates

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/de...mmatically.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.

Jan 20 '07 #1
1 16908
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=xxxxxx x;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");
}

}

Jan 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted...
3
by: Chad THomas | last post by:
I have a data repeater controll on a page. in each row that is displayed, I would like to have an action column that has differnet links shown depending on certain criteria from the database. In...
1
by: Simon | last post by:
Hi all, I have a repeater that is going to show a number of text boxes -two per row in fact. Does anyone know how I could go about ensuring that the user can't proceed without each and every...
5
by: Matt Jensen | last post by:
Am I right in saying that you can't have a Radiobutton web control inside a repeater bound to a database datasource and (inline) dynamically set it's ID and text properties from the repeaters rows?...
1
by: JD | last post by:
Hello Everyone, I am trying to do a page where when the user gets to the page it will look very similar to the following: <table border=1> <tr> <td bgcolor="green" colspan="2">Main...
3
by: Colin | last post by:
Hello, I can manage quite well in ASP but would like some advice in the best way to achieve dynamic layout in ASP.NET and still keep the page and code separate. Let's say I already have a...
1
by: Bobstar | last post by:
Hello Im trying to develop a nested repeatertemplate, Im trying to create a template in the following way: Repeater r = new Repeater(); r.ID = "Repeater0"; // r.HeaderTemplate = new...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
3
by: s.bussing | last post by:
Hi, I have been reading for hours now, but I'm still not able to solve my problem, though a lot of people are struggling with something similar. On my page I have a nested repeater. In the inner...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.