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.