使用 ASP.NET c# - Repeater 在 Div 中添加图像

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

我正在尝试在 div 中添加图像,该图像存在于代码隐藏的转发器控件中。

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

Inside above div 我正在尝试使用 inner 添加图像 HTML 属性:

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 未使用图像渲染。

在您的 DIV 中添加标签:

<div id="dvimage" runat="server" ... >
    <asp:Label ID="lblImage" runat="server"></asp:Label>
</div>

从你的代码隐藏:

foreach (RepeaterItem item in dtlViewUser.Items)
{
    Label lblImage = item.FindControl("lblImage") as Label;
    string imgupdated = "<img id='imgupdated' src='Images/active.png'  alt='updated' />";
    lblImage.Text = imgupdated;
}

更新:或者使用 ItemDataBound 事件:

protected void dtlViewUser_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
    // This event is raised for the header, the footer, separators, and items.

    // Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string imgupdated = "<img id=\"imgupdated\" src=\"Images/active.png\"  alt=\"updated\" />";

        ((Label)e.Item.FindControl("lblImage")).Text = imgupdated;
    }
}

尝试创建一个新的图像对象并将其包含在 DIV 的控件集合中:

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);
}