Creating Template Fields from code

  • Thread starter Henrik Skak Pedersen
  • Start date
H

Henrik Skak Pedersen

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

Steven Cheng[MSFT]

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top