Search code examples
asp.netgridviewtemplatefield

Simplifying ItemTemplate output of `GridView` to regular <td>


I'm using a TemplateField for a column because I need the HeaderTemplate. However, the ItemTemplate renders the content of a cell as an <asp:Label> and the output looks like this:

<td><span>data</span></td>

Is there any way to make the ItemTemplate just render the content of the cell so that the output will look like this:

<td>data</td>

Thanks for any suggestions.


Solution

  • The built in templates that are autogenerated will always use a Label for simplicity because they assume you might want to do formatting. If you want to just get basic HTML out switch it to use a Literal instead of a Label. A Literal acts almost the same as a Label with no formatting so there is no span tags. Change your TemplateField to the following:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal ID="ltTestItem" runat="server" Text="Test" />
        </ItemTemplate>
    </asp:TemplateField>
    

    It will produce:

    <td>Test</td>
    

    You can do the binding however you want by replacing the Text value with Eval("yourField") or by implementing the OnDataBinding for the control and manipulate it however you like.