Reputation: 1714
My item template in gridview is marked up like this.
Where do I add the
tags?
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% Eval("datacol") %>'
</asp:Label>
</ItemTemplate>
Upvotes: 1
Views: 2205
Reputation: 19797
Am I missing something?
Why not try:
<ItemTemplate>
<pre><%# Eval("datacol") %></pre>
</ItemTemplate>
It keeps the HTML in the ASPX page where it generally belongs, unless you specifically need the label tag.
Upvotes: 3
Reputation: 5348
Another alternative is to create your own custom control and place there
<ItemTemplate>
<cc1:MyCustomControl ID="MyCustomControl1" runat="server" Text='<% Eval("datacol") %>' />
</ItemTemplate>
Really, I don't know if it's better, maybe is a little expensive create a class/user control only for adding pre tags, but I think is less obstrusive at your code-behind file.
Upvotes: 0
Reputation: 44916
Since I realize this is related to your previous question: ASP.Net Binding to Gridview Strips some space (whitespace characters)
Please refer to my answer in that question. The same principle could be used to add any type of tags around your DataBound values. For this specific question, an approach like this could accomplish what you want.
protected void gbGridWithSpaces_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.Text = "<pre>" + cell.Text + "</pre>";
}
}
Upvotes: 0