如何使用ASP.NET C#将图像动态添加到Gridview数据绑定中



我需要在运行时将一个图像图标添加到特定单元格中的gridview数据绑定中。

我已经开始编写代码了,但我不确定如何完成,因为使用此代码时,图像不会显示在gridview中。。。

见下文:

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{                  
ImageButton image = new ImageButton();
image.ImageUrl = "C:/inetpub/wwwroot/img/add-page-red.gif";
e.Row.Cells[2].Controls.Add(image);
}
}

任何帮助都将不胜感激。。。非常感谢。

// In .aspx page use below code-
//for imageUrl you have to call the method which is defined in code behind-
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl='<%# GetImage() %>'/>
</ItemTemplate>
</asp:TemplateField>
// In code behind return the image path-
public static string GetImage()
{
return "../Images/Image.jpg";    
}

最新更新