Creating TemplateColumns for a grid at runtime

Discussion in 'ASP .Net' started by Jeremy Chapman, Jul 25, 2003.

  1. At run time I've added a TemplateColumn to a DataGrid.

    Now I'm trying to add a Table control to the TemplateColumns's
    HeaderTemplate and ItemTemplate.

    In essence, I'm trying to do in code, the equilavent of this html:

    <asp:TemplateColumn>

    <HeaderTemplate>

    <asp:Table>

    <asp:TableRow>

    <asp:TableCell>

    </asp:TableRow>

    <asp:TableRow>

    <asp:TableCell>

    </asp:TableRow>

    <asp:Table>

    </HeaderTemplate>

    <ItemTemplate>
    <asp:Table>

    <asp:TableRow>

    <asp:TableCell>

    </asp:TableRow>

    <asp:TableRow>

    <asp:TableCell>

    </asp:TableRow>

    <asp:Table>

    </ItemTemplate>

    </asp:TemplateColumn>
    Jeremy Chapman, Jul 25, 2003
    #1
    1. Advertising

  2. I'm trying to do just that at the moment. It's a little involved. What you
    do is put everything between the <HeaderTemplate> and </HeaderTemplate> tags
    (not the tags) into an .ascx file, like a user control. Then
    you load that file into your template column.
    http://www.dotnetbips.com/displayarticle.aspx?id=84

    Trouble is, I find this method really slow. There's an alternative which is
    even more complicated:
    http://www.dotnetbips.com/displayarticle.aspx?id=85

    Let me know if you learn a way that performs well ...

    Good luck

    Justin Dutoit

    "Jeremy Chapman" <> wrote in message
    news:#...
    > At run time I've added a TemplateColumn to a DataGrid.
    >
    > Now I'm trying to add a Table control to the TemplateColumns's
    > HeaderTemplate and ItemTemplate.
    >
    > In essence, I'm trying to do in code, the equilavent of this html:
    >
    > <asp:TemplateColumn>
    >
    > <HeaderTemplate>
    >
    > <asp:Table>
    >
    > <asp:TableRow>
    >
    > <asp:TableCell>
    >
    > </asp:TableRow>
    >
    > <asp:TableRow>
    >
    > <asp:TableCell>
    >
    > </asp:TableRow>
    >
    > <asp:Table>
    >
    > </HeaderTemplate>
    >
    > <ItemTemplate>
    > <asp:Table>
    >
    > <asp:TableRow>
    >
    > <asp:TableCell>
    >
    > </asp:TableRow>
    >
    > <asp:TableRow>
    >
    > <asp:TableCell>
    >
    > </asp:TableRow>
    >
    > <asp:Table>
    >
    > </ItemTemplate>
    >
    > </asp:TemplateColumn>
    >
    >
    Justin Dutoit, Jul 25, 2003
    #2
    1. Advertising

  3. I'm going to try the second solution. Looks like it could be a good one.

    "Justin Dutoit" <> wrote in message
    news:...
    > I'm trying to do just that at the moment. It's a little involved. What you
    > do is put everything between the <HeaderTemplate> and </HeaderTemplate>

    tags
    > (not the tags) into an .ascx file, like a user control. Then
    > you load that file into your template column.
    > http://www.dotnetbips.com/displayarticle.aspx?id=84
    >
    > Trouble is, I find this method really slow. There's an alternative which

    is
    > even more complicated:
    > http://www.dotnetbips.com/displayarticle.aspx?id=85
    >
    > Let me know if you learn a way that performs well ...
    >
    > Good luck
    >
    > Justin Dutoit
    >
    > "Jeremy Chapman" <> wrote in message
    > news:#...
    > > At run time I've added a TemplateColumn to a DataGrid.
    > >
    > > Now I'm trying to add a Table control to the TemplateColumns's
    > > HeaderTemplate and ItemTemplate.
    > >
    > > In essence, I'm trying to do in code, the equilavent of this html:
    > >
    > > <asp:TemplateColumn>
    > >
    > > <HeaderTemplate>
    > >
    > > <asp:Table>
    > >
    > > <asp:TableRow>
    > >
    > > <asp:TableCell>
    > >
    > > </asp:TableRow>
    > >
    > > <asp:TableRow>
    > >
    > > <asp:TableCell>
    > >
    > > </asp:TableRow>
    > >
    > > <asp:Table>
    > >
    > > </HeaderTemplate>
    > >
    > > <ItemTemplate>
    > > <asp:Table>
    > >
    > > <asp:TableRow>
    > >
    > > <asp:TableCell>
    > >
    > > </asp:TableRow>
    > >
    > > <asp:TableRow>
    > >
    > > <asp:TableCell>
    > >
    > > </asp:TableRow>
    > >
    > > <asp:Table>
    > >
    > > </ItemTemplate>
    > >
    > > </asp:TemplateColumn>
    > >
    > >

    >
    >
    Jeremy Chapman, Jul 25, 2003
    #3
  4. It's OK now, tks

    Justin

    "Justin Dutoit" <> wrote in message
    news:...
    > Would you help me with something- I'll show you the command-line compiler
    > line followed by the code... I'm missing a reference or a 'using'

    directive
    > which 'Container' needs, maybe you know which one...
    >
    > csc /out::..\bin\templatecolumns.dll /t:library /r:System.Data.dll
    > /r:System.dll /r:System.Web.dll ColumnTemplate.cs
    >
    >
    > ColumnTemplate.cs:
    >
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Data;
    >
    >
    > namespace Quickshop
    > {
    >
    > public class MyTemplateColumn:ITemplate
    > {
    > private string colname;
    >
    > public MyTemplateColumn(string cname)
    > {
    >
    > colname=cname;
    >
    > }
    >
    > /*******************************************************************
    > ** As a template, the class must implement the following method **
    > *******************************************************************/
    >
    > public void InstantiateIn(Control container)
    > {
    >
    > LiteralControl l = new LiteralControl();
    > l.DataBinding +=
    > new EventHandler(this.OnDataBinding);
    > container.Controls.Add(l);
    >
    > }
    >
    >
    > public void OnDataBinding(object sender, EventArgs e)
    > {
    >
    > DataRowView mydataitem = (DataRowView)container.Dataitem;
    > LiteralControl l = (LiteralControl) sender;
    > DataGridItem container = (DataGridItem) l.NamingContainer;
    > l.Text = "<SPAN class=header>It is " +
    > ((DataRowView)container.DataItem)[colname].ToString() + "!</SPAN>";
    >
    > // l.Text = "<SPAN class=header>It si " +
    > // (mydataitem)["productnumber"].ToString() +
    > // (mydataitem)["brand"].ToString() +
    > // (mydataitem)["productname"].ToString() +
    > // (mydataitem)["price"].ToString() + "!</SPAN>";
    >
    >
    > }
    >
    > }
    >
    > }
    >
    >
    > Cheers
    > Justin
    >
    >
    >
    > "Jeremy Chapman" <> wrote in message
    > news:e3GZ#...
    > > I'm going to try the second solution. Looks like it could be a good

    one.
    > >
    > > "Justin Dutoit" <> wrote in message
    > > news:...
    > > > I'm trying to do just that at the moment. It's a little involved. What

    > you
    > > > do is put everything between the <HeaderTemplate> and

    </HeaderTemplate>
    > > tags
    > > > (not the tags) into an .ascx file, like a user control. Then
    > > > you load that file into your template column.
    > > > http://www.dotnetbips.com/displayarticle.aspx?id=84
    > > >
    > > > Trouble is, I find this method really slow. There's an alternative

    which
    > > is
    > > > even more complicated:
    > > > http://www.dotnetbips.com/displayarticle.aspx?id=85
    > > >
    > > > Let me know if you learn a way that performs well ...
    > > >
    > > > Good luck
    > > >
    > > > Justin Dutoit
    > > >
    > > > "Jeremy Chapman" <> wrote in message
    > > > news:#...
    > > > > At run time I've added a TemplateColumn to a DataGrid.
    > > > >
    > > > > Now I'm trying to add a Table control to the TemplateColumns's
    > > > > HeaderTemplate and ItemTemplate.
    > > > >
    > > > > In essence, I'm trying to do in code, the equilavent of this html:
    > > > >
    > > > > <asp:TemplateColumn>
    > > > >
    > > > > <HeaderTemplate>
    > > > >
    > > > > <asp:Table>
    > > > >
    > > > > <asp:TableRow>
    > > > >
    > > > > <asp:TableCell>
    > > > >
    > > > > </asp:TableRow>
    > > > >
    > > > > <asp:TableRow>
    > > > >
    > > > > <asp:TableCell>
    > > > >
    > > > > </asp:TableRow>
    > > > >
    > > > > <asp:Table>
    > > > >
    > > > > </HeaderTemplate>
    > > > >
    > > > > <ItemTemplate>
    > > > > <asp:Table>
    > > > >
    > > > > <asp:TableRow>
    > > > >
    > > > > <asp:TableCell>
    > > > >
    > > > > </asp:TableRow>
    > > > >
    > > > > <asp:TableRow>
    > > > >
    > > > > <asp:TableCell>
    > > > >
    > > > > </asp:TableRow>
    > > > >
    > > > > <asp:Table>
    > > > >
    > > > > </ItemTemplate>
    > > > >
    > > > > </asp:TemplateColumn>
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Justin Dutoit, Jul 26, 2003
    #4
    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. =?Utf-8?B?VGltOjouLg==?=

    Problem with datagrid and TemplateColumns

    =?Utf-8?B?VGltOjouLg==?=, Jul 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    305
    Joe Fallon
    Jul 28, 2004
  2. Jim Bancroft
    Replies:
    3
    Views:
    362
  3. Replies:
    2
    Views:
    11,459
    =?Utf-8?B?Y2xpY2tvbg==?=
    Mar 30, 2006
  4. tshad
    Replies:
    5
    Views:
    494
    Alexey Smirnov
    Aug 31, 2007
  5. Pop

    Dynamic TemplateColumns lose modified values on postback

    Pop, Feb 4, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    173
Loading...

Share This Page