dynamic table and repeater at run-time

  • Thread starter Mr Not So Know It All
  • Start date
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");
}

}
 
E

Eliyahu Goldin

Repeater repeats its itemtemplate for every bound item. What are you trying
to achieve? There should not be any need for adding things in codebehind,
you should be able to manage with the markup.
 
M

Mr Not So Know It All

thanks for responding to my question. i noticed a few of the examples
in the MS site populate the repeater with a label control via
codebehind. i thought it would be possible to populate the repeater
with a table via the codebehind. is there a way to do this? or should i
not even try? thanks again for the reply.
 
M

Mr Not So Know It All

thanks for your replay again, Eliyahu .

i've figured out how to build and populate the repeater with a table
with markup. i would like to build and populate the repeater at
runtime.

as you see in my code example above, im able to add the table to the
itemtemplate at runtime. also, im able to databind the data to the
table cell. unfortunately, cell is being filled with all of the data.
in the MS example from this link, the label text property is being
filled with one record and then moves to the next. the label control
also includes "<table>" code. i was hoping how to build a table with
table controls and add various other controls to it at runtime (for
example a button control or dropdown depending on the user input).

creating web control templates programmatically..
http://msdn.microsoft.com/library/d...webservercontroltemplatesprogrammatically.asp

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

can you help me figure out how i can populate each table cell with one
data record as in the example from the MS site.

again, thanks for your response and input
 
M

Mr Not So Know It All

here is the databinding part of the code example from the MS site.

// C#
private void TemplateControl_DataBinding(object sender,
System.EventArgs e)
{
Literal lc;
lc = (Literal) sender;
RepeaterItem container = (RepeaterItem) lc.NamingContainer;
lc.Text += DataBinder.Eval(container.DataItem, "CategoryName");
lc.Text += "</TD></TR>";
}

as you can see, it binds the literal control text property. however, it
looks like it binds one record at a time. is there a way i can do that
with other controls?

thanks again for helping.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top