Datagrid dynamically generated template columns dissapearing

A

abigblackman

Hi,

I have a datagrid that is programatically generated with template
columns.
There seems to be something happening where only the last record is
rendered with any data.

I start by creating the template columns like this:
<code>
TemplateColumn tc.HeaderText = "Name";

ColumnTemplate ct = new ColumnTemplate ();
Label lbl = new Label ();
lbl.ID = "lblName";

ct.SetControl ( lbl );
tc.ItemTemplate = ct;

dgResults.Columns.Add ( tc );
</code>
Where ColumnTemplate looks like:
<code>
class ColumnTemplate : ITemplate {

Control con;

public void InstantiateIn ( Control container ) {
container.Controls.Add ( con );
}

public void SetControl ( Control con ) {
this.con = con;
}

}

</code>

Finally when each item is bound to the column I do something like:
<code>
// name
lbl = (Label)e.Item.FindControl ( "lblName" );
lbl.Text = person.LastName + ", " + person.FirstName;
</code>

When stepping through the debugger I can see the correct value being
assigned to the label.

However when I view the output the only data is contained in the last
row. All the previous rows are empty eg:
<code>
<table cellspacing="0" rules="all" border="1"
style="border-collapse:collapse;">
<tr>
<td>Name</td><td>Company</td>
</tr><tr>
<td></td><td></td>
</tr><tr>
<td></td><td></td>
</tr><tr>
<td><span id="ctl00_pageContent_ctl00_ctl04_lblName">My
Name</span></td><td><span
id="ctl00_pageContent_ctl00_ctl04_lblCompany">My Company</span></td>
</tr>
</table>
</code>

Any ideas what might be going on here?

Cheers,

Marshall
 
B

Brock Allen

Just like any other dynamically created control, make sure you recreate them
upon every postback.
 
A

abigblackman

Hi Brock,

It's definatly recreated. All the rows are showing but they are all
empty except for the very last row which contains the correct data.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top