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