views:

251

answers:

3

I have a td that I want to inject with a server image control (asp.net) using innerHTML = "". The webcontrol's toString is giving the type.

Is there a way to extract the generated from the server control? Or, is there a different solution...?

Thanks

+1  A: 
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
img.RenderControl(new HtmlTextWriter(writer));
td.InnerHtml = sb.ToString();

or the more obvious

td.Controls.Add(img);
Al W
A: 

The first part of your question looks like you're asking how to inject an image at runtime into a table cell...

If the table cell is part of your ASP.NET page, you could do something like:

<td id="imageCell" runat="server"/>

In your code behind:

Image img = new Image();
img.ImageUrl = "mypic.jpg";
imageCell.Controls.Add(img);

HTH
Kev

Kev
A: 

You can use the RenderControl method to get the output HTML.

Ex:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

class Program
{
    static void Main()
    {
        var img = new Image();

        var hw = new HtmlTextWriter(Console.Out);
        img.RenderControl(hw);

        hw.Dispose();
    }
}

Output:

<img src="" style="border-width:0px;" />
chakrit