0

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.

1
  • Why are you referencing drive c? It should look like image.ImageUrl = "img/add-page-red.gif"; Commented Mar 19, 2021 at 11:58

1 Answer 1

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";    
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.