Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Using TemplateColumn and DataGrid in code

Reply
Thread Tools

Using TemplateColumn and DataGrid in code

 
 
=?Utf-8?B?TXJDcm9vbA==?=
Guest
Posts: n/a
 
      08-04-2006
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.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TXJDcm9vbA==?=
Guest
Posts: n/a
 
      08-07-2006
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.


"MrCrool" wrote:

> 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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TXJDcm9vbA==?=
Guest
Posts: n/a
 
      08-07-2006
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


"MrCrool" wrote:

> 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.
>
>
> "MrCrool" wrote:
>
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid, templateColumn, custom controls and dynamic data binding Chu ASP .Net 0 04-13-2006 10:22 PM
Create TemplateColumn on datagrid exclusively with code behind Ricardo ASP .Net Datagrid Control 2 04-11-2005 08:33 PM
Create TemplateColumn on datagrid exclusively with code behind Ricardo ASP .Net 2 04-11-2005 08:33 PM
Hide datagrid TemplateColumn w/out using Cells collection =?Utf-8?B?QmlsbCBCb3Jn?= ASP .Net 0 10-21-2004 10:13 PM
Make DataGrid TemplateColumn read-only in code Bob ASP .Net 0 07-09-2003 05:56 PM



Advertisments