Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I created an HTML table dynamically.Then I wanted to embed a server image in the cell of that table.I did so as-

HtmlTableCell c = new HtmlTableCell();
Image img = new Image();
img.ImageUrl = Server.MapPath("Images/red.jpg");
c.Controls.Add(img); 

But when I execute this, the image is not appearing.
Am I missing something over here?

Please suggest.

Thanks in advance !!
[edit]Code block addded, "Ingore HTML..>" option disabled - OriginalGriff[/edit]
Posted
Updated 25-Feb-11 12:40pm
v4

Try using the following code, hope it will work.
HtmlTable tab = new HtmlTable();
tab.Border = 1;
tab.BorderColor = "#cccccc";
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell c = new HtmlTableCell();
Image img = new Image();
img.ImageUrl=Page.ResolveClientUrl("~/Images/red.jpg");
img.Style.Add("width", "300px;");
img.Style.Add("height", "400px");
c.Controls.Add(img);
c.Style.Add("width", "300px;");
c.Style.Add("height", "400px;");
tr.Cells.Add(c);
tab.Rows.Add(tr);
form1.Controls.Add(tab);


If i misunderstand your question, please feel free to correct me.I hope the above information will be helpful. If you have more concerns, please let me know.

If this would be really helpful to you then don't forgot to Vote and Make Answer as Accepted.
 
Share this answer
 
v4
Comments
rajivpande86 25-Feb-11 17:40pm    
Thanx bro !! It helped. I guess ResolveClientUrl did the trick. Cheers !!
Espen Harlinn 25-Feb-11 17:46pm    
Nicely spotted, my 5 for img.ImageUrl=Page.ResolveClientUrl
Yes.


Oh, you want more details?
C#
HtmlTableCell c = new HtmlTableCell();
Creates a new instance of a table cell :thumbsup:
C++
Image img = new Image();
Creates a new image control. :thumbsup:
C++
img.ImageUrl = Server.MapPath("Images/red.jpg");
Points the Image control at an image file :thumbsup:
C++
c.Controls.Add(img);
Adds the Image to the Table cell.:thumbsup:

And then does nothing with the table cell to try and display it....:thumbsdown:
 
Share this answer
 
Comments
rajivpande86 25-Feb-11 17:43pm    
I didn't include the rest of the code because I was sure there won't be a glitch in that part.Thanx a lot for your efforts on dealing with this.

Regards
Rajeev
Did you add the table cell to the table row and then the table row to the table cell? By creating a new HtmlTableCell, it isn't attached to any of the parent objects that it needs to be.
I ask because your snippet doesn't indicate that you have done that part successfully.
 
Share this answer
 
Comments
rajivpande86 25-Feb-11 17:42pm    
Yes, I had done that. I didn't include it because I was sure there won't be a glitch in that part.Still thanx a lot for your efforts on dealing with this.

Regards
Rajeev

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900