4

I have an ASP.NET link button that I need to add both text and an image to I thought I would be able to just add the image to the button using Controls.Add but no dice.

Here is what I have so far:

foreach (var entity in metadata.Meta.Where(e => e.ReportableObject == true))
{
    LinkButton lb = new LinkButton();
    string iconpath = "~/images/search/" + entity.ClassName + ".png";
    lb.CssClass = "entityIcon";
    lb.Controls.Add(new Image { ImageUrl =  iconpath , CssClass = "imgSize"});
    lb.Text = entity.ClassName;
    entityHolder.Controls.Add(lb);
}

When the control renders I see the text but I'm not even getting an image container rendered. I thought it might have been the path to the image but even when I tried to map to a image from an existing site using the full qualified path nothing would render.

Edit: For clarification there are no asp.net controls in the main page (link or image) this for loop is iterating a collection and creating the controls at runtime. The last line entityHolder is a ASP.NET panel that the buttons are added to.

Can someone help me understand what I am doing wrong if this is even possible. I have to use a LinkButton and not an ImageButton as I need to render both text and image.

Cheers

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
rlcrews
  • 3,482
  • 19
  • 66
  • 116

2 Answers2

0

Use the background css attribute instead of adding a new control.

 LinkButton lb = new LinkButton;
 string iconpath = "~/images/search/" + entity.ClassName + ".png";

 lb.Style.Add("background", "url('" + base.ResolveUrl(iconpath) + "') left center no-repeat");

 lb.CssClass = "entityIcon";
 lb.Text = entity.ClassName;
 entityHolder.Controls.Add(lb);
Francis P
  • 13,377
  • 3
  • 27
  • 51
  • @Francis The link buttons are being generated at runtime and added to a panel control (entityHolder). I've updated my question to reflect this. As a result there are no buttons or images in the aspx file to reference. That is why I need to handle all this in code (generation and assignment) -thnx – rlcrews Dec 19 '12 at 21:01
0

You would have to do it manipulating the Text property of the control to also render the <img tag for example:

foreach (var entity in metadata.Meta.Where(e => e.ReportableObject == true))
{
      LinkButton lb = new LinkButton();
      string iconpath = ResolveClientUrl("~/images/search/" + entity.ClassName + ".png");
      lb.CssClass = "entityIcon";
      lb.Text = string.Format(@"image <img src=""{0}"" class=""{1}"" />",iconpath,"imgSize");
      entityHolder.Controls.Add(lb);
}
rlcrews
  • 3,482
  • 19
  • 66
  • 116
Icarus
  • 63,293
  • 14
  • 100
  • 115