Using TemplateColumn and DataGrid in code

G

Guest

Hi

I need to be able to handle the following ASP programming in pure C# code:

<asp:TemplateColumn HeaderText="Customer Information">
<ItemTemplate>
<table border="0">
<tr>
<td align="right"><b>Name:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "name") %></td>
</tr>
<tr>
<td align="right"><b>ID number:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "customerID") %></td>
</tr>
<tr>
<td align="right"><b>E-mail:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "email") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>

I want to be able to create TemplateColumns that contain some HTML and be
able to insert multiple values from my datasource at runtime.

The reason for this is I want it to be part of a WebPart, where the DataGrid
is added through the CreateChildControls() method.
 
G

Guest

Finally I also found a solution to this, posting it here for reference if
anyone else ever needs to do something similar. I involves using
TemplateColumn and DataGrid to output several generated rows with presence
information.

For doing this in code, like I did, this link explains a lot of the code in
using the TemplateColumn:
http://www.superdotnet.com/Article.aspx?ArticleID=116

To get the E-mail for presence detection and name for display from the
datasource, the following example can be used (C#):

public class MyColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Label name = new Label();
name.DataBinding += new EventHandler(myDatSource_DataBinding);
container.Controls.Add(name);
}

public void myDatSource_DataBinding(object sender, EventArgs e)
{
Label name = ((Label)(sender));
DataGridItem container = ((DataGridItem)(name.NamingContainer));
string email =
Convert.ToString(DataBinder.Eval((((DataGridItem)(container))).DataItem,
"eMail"));
}
}//End class


This is a simple example to show how this would work. Here you only see the
"name" attribute as the output. The rest would just be added just like this
is.
Notice that you need to add an event handler that gets the source to make
this work, something that ASP.NET does automatically.
 
G

Guest

Actually, an even better example would be like this:


public class MyColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
LiteralControl ouputLiteral = new LiteralControl();
ouputLiteral.DataBinding += new EventHandler(myLiteral_DataBinding);
container.Controls.Add(ouputLiteral);
}

public void myLiteral_DataBinding(object sender, EventArgs e)
{
LiteralControl myLiteral = ((LiteralControl)(sender));
DataGridItem container = ((DataGridItem)(myLiteral.NamingContainer));
string email =
Convert.ToString(DataBinder.Eval((((DataGridItem)(container))).DataItem,
"eMail"));
string name =
Convert.ToString(DataBinder.Eval((((DataGridItem)(container))).DataItem,
"contactName"));
string idnumber =
Convert.ToString(DataBinder.Eval((((DataGridItem)(container))).DataItem,
"idNumber"));
string strExactOutput;

strExactOutput = "" +
"<table border=\"0\">" +
"<tr>" +
"<td align=\"right\"><b>Name:</b></td>" +
"<td>" + name + "</td>" +
"</tr>" +
"<tr>" +
"<td align=\"right\"><b>ID number:</b></td>" +
"<td>" + idnumber + "</td>" +
"</tr>" +
"<tr>" +
"<td align=\"right\"><b>E-mail:</b></td>" +
"<td>" + email +
"</td>" +
"</tr>" +
"</table>";

myLiteral.Text = strExactOutput;
}
}//End class
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top