Search code examples
asp.netevalitemtemplate

How to use codeblocks in itemtemplates


I have a gridview with several BoundField. Their datafield is set statistically but I have one field that requires an eval to bind to a field dynamically. As BoundField doesn't support Eval, I've added one TemplateField. Here's an example:

                                        <asp:TemplateField HeaderText="<%$ Resources:Resource, Type%>" SortExpression="Description">
                                            <ItemStyle CssClass="gridColumn_Header" />
                                            <HeaderStyle CssClass="gridColumn_Header" />
                                            <ItemTemplate>
                                                <asp:Literal Text='<%# Eval('StreetType.Description') %>'/>
                                            </ItemTemplate>
                                        </asp:TemplateField>

However, I'm getting an error saying that code blocks aren't supported in this context.


Solution

  • It appears that codeblocks aren't allowed inside <asp: runat="server">, which is odd. The grid is nested inside several panel and update panels, all of which requires runat to get a reference in codebehind.

    Although I found the answer to my question, I'm left without a solution.