Creating Template Fields from code

Discussion in 'ASP .Net Datagrid Control' started by Henrik Skak Pedersen, Dec 28, 2006.

  1. Hi,

    I have a template field looking like this:

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Image ID="Image1" Width="75" Height="100"
    ImageUrl='<%# GetURL(DataBinder.Eval(Container.DataItem,
    "Picture") as System.Byte[]) %>' Runat=server />
    </ItemTemplate>
    </asp:TemplateField>

    I would like to create this template field from code and I have tried the
    following:

    ------------
    TemplateField template = new TemplateField();
    template.ItemTemplate = new ImageTemplate();
    template.HeaderText = "Picture";
    this.Grid.Columns.Add(template);
    ------

    public class ImageTemplate : ITemplate
    {
    public void InstantiateIn(Control container)
    {
    Image img = new Image();
    img.ID = "EmployeePicture";
    img.Width = 75;
    img.Height = 100;
    // img.ImageUrl = "<%# GetURL(DataBinder.Eval(Container.DataItem,
    \"Picture\") as System.Byte[]) %>";
    container.Controls.Add(img);
    }
    }
    ------

    My problem is how to do I set the ImageUrl. If I do it like above I get an
    invalid URL. Any ideas?

    I can get it to work if I subscribe to the RowDataBound event on the grid
    and set the url there like below. But I prefer to do it the other way.

    protected override void RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Employee employee = (Employee)e.Row.DataItem;
    Image image = (Image)e.Row.FindControl("EmployeePicture");
    image.ImageUrl = GetURL(employee.Picture as System.Byte[]);
    }
    }

    Thanks
    Henrik.
    Henrik Skak Pedersen, Dec 28, 2006
    #1
    1. Advertising

  2. Hello Henrik,

    As for the programmatic TemplateField(with databinding expression) creating
    problem you met, I think you can consider the following means to resolve it:

    1. You can save the template field's html markup content in a separate file
    and then load it in code later. e.g.

    ======template content file======
    ....................
    <asp:Image ID="Image1" Width="75" Height="100"
    ImageUrl='<%# GetURL(DataBinder.Eval(Container.DataItem,
    "Picture") as System.Byte[]) %>' Runat=server />
    ...............
    ====================

    In your templateField creation code, you can load the template as below:

    ======================
    TemplateField tf1 = new TemplateField();
    tf1.HeaderText = "template column";
    tf1.ItemTemplate = Page.LoadTemplate("~/templates/template.ascx");

    GridView1.Columns.Add(tf1);
    ======================
    #template.ascx here is not a usercontrol, just a template file contains the
    ItemTemlate's content.


    2. If you do not want to use a separate template content file, and insist
    to using " InstantiateIn" method of Itemplate, you need to register
    DataBinding event for any control(in the templateField) that need value
    from DataBound source. e.g.

    ==========InstantiateIn=========
    ..............
    Label lblName = new Label();
    lblName.DataBinding += new EventHandler(this.BindName);
    container.Controls.Add(lblName);

    ===============

    ======================
    void BindName(Object sender, EventArgs e)
    {
    Label l = (Label) sender;
    DataGridItem container;
    container = (DataGridItem) l.NamingContainer;

    DataRowView drv;
    drv = ((DataRowView) container.DataItem);
    l.Text = drv["lastname"].ToString();
    }

    ================

    both of the two approaches has been well described in the followign tech
    article:

    #Understanding Templates in ASP.NET
    http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/

    Please feel free to let me know if there is anything unclear.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Dec 29, 2006
    #2
    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. call_me_anything
    Replies:
    4
    Views:
    454
    Pete Becker
    Sep 30, 2007
  2. Replies:
    0
    Views:
    403
  3. Cyril.Liu
    Replies:
    0
    Views:
    437
    Cyril.Liu
    Dec 2, 2008
  4. nuve
    Replies:
    1
    Views:
    126
  5. middletree

    how to copy from fields to other fields

    middletree, Nov 5, 2003, in forum: ASP General
    Replies:
    1
    Views:
    154
    middletree
    Nov 5, 2003
Loading...

Share This Page