Question

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

Was it helpful?

Solution

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top