简体   繁体   中英

How can I add image into Gridview Databound dynamically using ASP.NET C#

I need to add an image icon to gridview databound at runtime in a specific cell.

I started code already but i'm not sure how to accomplish it because using this code the image don't showing in gridview...

See below:

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

Any help would greatly appreciate... Thank you.

// 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";    
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM