Search code examples
c#htmlasp.netrepeater

Add Image inside Div using ASP.NET c# - Repeater


I am trying to add an image inside a div which is present in repeater control from code behind.

<div id="dvimage"
     runat="server"
     style="width:20px;float:right;margin-right:22%;margin-top:-18px;">
  </div>

Inside above div I am trying to add an image using inner HTML property:

foreach (RepeaterItem item in dtlViewUser.Items)
{
    HtmlGenericControl sortImagedv = item.FindControl("dvimage") as HtmlGenericControl;
    string imgupdated=  "<img id=\"imgupdated\" src=\"Images/active.png\"  alt=\"updated\" />";
    sortImagedv.InnerHtml = imgupdated; 
}

div is not rendered with the Image.

enter image description here


Solution

  • Try creating a new image object and including it in the DIV's control collection:

    foreach (RepeaterItem item in dtlViewUser.Items)
    {
        HtmlGenericControl sortImagedv = item.FindControl("dvimage") as HtmlGenericControl;
        HtmlImage image = new HtmlImage();
        image.Src = "Images/active.png";
        image.Alt = "updated";
        image.ID = "imgupdated";
    
        sortImagedv.Controls.Add(image);
    }