2

in asp.net, I'm trying to generate an image tag with:

var img = new HtmlGenericControl("img");

it seems to generate <img></img>

How would I generate a normal image tag with asp.net/C#?

e.g.

<img/>
tester
  • 22,441
  • 25
  • 88
  • 128
  • 2
    Semantically `` is the same as `` so why does it matter? – Bala R Jun 08 '11 at 17:41
  • possible duplicate of [Self closing Html Generic Control?](http://stackoverflow.com/questions/1708037/self-closing-html-generic-control) – Bala R Jun 08 '11 at 17:42
  • 1
    @Bala R, you're correct, it is a similar issue. I'd like to think this question is specific enough to remain. Upon observing the first answer, it seems that using HtmlGenericControl is not the best way to create img tags. – tester Jun 08 '11 at 17:45
  • @Bala R, in my specific use case, IE6 and 7 don't behave consistently when using vs – tester Jun 08 '11 at 17:48
  • I see. In that case you'll have to create a subclass from `HtmlGenericControl` and override `Render` as shown in this post http://www.blackbeltcoder.com/Articles/aspnetcontrols/a-self-closing-htmlgenericcontrol – Bala R Jun 08 '11 at 17:53

3 Answers3

6

There's a specialized html control for the image element.

System.Web.UI.HtmlControls.HtmlImage

which generates the html you want.

lincolnk
  • 11,218
  • 4
  • 40
  • 61
  • this seems like the easiest fix. I just changed each instance of `HtmlGenericControl("img")` to `HtmlImage()` and everything worked as planned. Thanks – tester Jun 08 '11 at 18:00
1

You are creating it as a generic html control. Try using the Image control:

var img = new Image();
Robert Beaubien
  • 3,126
  • 4
  • 22
  • 29
1

Override the rendering method and use HtmlTextWriter.SelfClosingTagEnd to create a self closing tag?

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32