views:

381

answers:

3

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>
A: 

Since I realize this is related to your previous question: http://stackoverflow.com/questions/924058/asp-net-binding-to-gridview-strips-some-space-whitespace-characters/924115#924115

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>";
    }
}
Josh
A: 

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.

Matias
It might be helpful if you include how to expose the Text property as a bindable attribute as well. If you follow the link to his first question you might understand why I suggest this...
Josh
+1  A: 

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.

Jon P