Using TemplateColumn and DataGrid in code

Discussion in 'ASP .Net' started by =?Utf-8?B?TXJDcm9vbA==?=, Aug 4, 2006.

  1. 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.
    =?Utf-8?B?TXJDcm9vbA==?=, Aug 4, 2006
    #1
    1. Advertising

  2. 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.
    =?Utf-8?B?TXJDcm9vbA==?=, Aug 7, 2006
    #2
    1. Advertising

  3. 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.
    =?Utf-8?B?TXJDcm9vbA==?=, Aug 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    0
    Views:
    603
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Hide datagrid TemplateColumn w/out using Cells collection

    =?Utf-8?B?QmlsbCBCb3Jn?=, Oct 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,640
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Oct 21, 2004
  3. Ricardo
    Replies:
    2
    Views:
    6,781
    David Alpert
    Apr 11, 2005
  4. Chu
    Replies:
    0
    Views:
    505
  5. Ricardo
    Replies:
    2
    Views:
    141
    David Alpert
    Apr 11, 2005
Loading...

Share This Page