【问题标题】:How can I add image into Gridview Databound dynamically using ASP.NET C#如何使用 ASP.NET C# 动态地将图像添加到 Gridview Databound
【发布时间】:2021-06-16 19:50:15
【问题描述】:

我需要在运行时将图像图标添加到特定单元格中的 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);
    }
}

任何帮助将不胜感激...谢谢。

【问题讨论】:

  • 为什么要引用驱动器 c?它应该看起来像 image.ImageUrl = "img/add-page-red.gif";
  • @BorisSokolov 感谢您的帮助...

标签: c# asp.net image gridview rowdatabound


【解决方案1】:
// 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";    
     }

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多