我试图在div
中添加一个图像,该图像存在于代码后面的转发器控件中。
<div id="dvimage"
runat="server"
style="width:20px;float:right;margin-right:22%;margin-top:-18px;">
</div>
在div
上面我试图使用内部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
不会使用Image进行渲染。
尝试创建一个新的图像对象并将其包含在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);
}
在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;
}
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;
}
}