I have two types of code: 1st:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" Text="Скачать объект" NavigateUrl='<%#"objects/" + Eval("Идентификатор") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
works normal. But TemplateField showed everytime.
2nd
TemplateField templField = new TemplateField();
HyperLink hypLink = new HyperLink();
hypLink.NavigateUrl = "<%#\"objects/\" + Eval(\"Идентификатор\") %>";
hypLink.Text = "Скачать объект";
templField.InsertItemTemplate = (ITemplate)hypLink;
tableResults.Columns.Add(templField);
dosn't work with error: Unable to cast object of type 'System.Web.UI.WebControls.HyperLink' to type 'System.Web.UI.ITemplate'. Why in 1st time HyperLink added, in 2nd time didn't?
This might help to get started:
Note that you cannot set the
NavigateUrl
orText
from withinInstantiateIn
. There you only create the control. You would databind it inRowDataBound
according to the row'sDataItem
.But:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx
templField.InsertItemTemplate
expects anITemplate
object where as hyperlink does not extends this interface.In your first declarative example, hyperlink is a host of
ItemTemplate
which extend this interfaceITemplate
.