Reputation:
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
Upvotes: 0
Views: 1673
Reputation: 119846
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);
Upvotes: 0
Reputation: 61548
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;" />
Upvotes: 0
Reputation: 7713
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);
Upvotes: 1