Search code examples
c#asp.netgridviewtemplatefield

Asp.net Gridview templatefield line break not working


I have an image and a label in a gridview templatefield.

<asp:TemplateField HeaderText = "Rate" >
    <ItemTemplate>
       <asp:Image ID="imgID" runat="server" />
        <asp:Label id="ImageNameLabel" Text="" runat="server" /> 
     </ItemTemplate>
</asp:TemplateField>

CODE BEHIND

if (e.Row.RowType == DataControlRowType.DataRow)
{
    Image img = (Image)e.Row.FindControl("imgID");
    img.ImageUrl = "~/Images/s1.png";
    img.Height = 80;
    img.Width = 80;
    e.Row.Cells[6].Controls.Add(img);
    Label lbl = (Label)e.Row.FindControl("ImageNameLabel")  
    lbl.Text = "Image Name Here";
}

When i run the application, both image and label are showing right next to each other. I want the label to be right below the image.

If i add this

lbl.Text = "Image Name Here" + "<br/>";

then it will be the other way around (label on top, image at the bottom).

How can i move the label right below the image?


Solution

  • Try this and it should work.

    <asp:TemplateField HeaderText = "Rate" >
    <ItemTemplate>
    <table>
    <tr>
      <td align="center">
       <asp:Image ID="imgID" runat="server" />
      </td>
    </tr>
    <tr>
      <td>
        <asp:Label id="ImageNameLabel" Text="" runat="server" /> 
      </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:TemplateField>