Is there a way I could dynamically add a Image1 to the while loop in the below code (contained within the div) By this I mean actually adding an asp image to the div? via the code. At the moment as I see it the code looks for an asp image but Ive seen no way you can “add” it to my dynamic content:
using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
var divHtml = new System.Text.StringBuilder();
while (reader.Read())
{
divHtml.Append("<div id=test>");
divHtml.Append(String.Format("{0}", reader.GetString(0)));
divHtml.Append("</div>");
}
test1.InnerHtml = divHtml.ToString();
}
}
Thought I had it with this:
var divHtml = new System.Text.StringBuilder();
while (reader.Read())
{
divHtml.Append("<div id=test>");
divHtml.Append(String.Format("{0}", reader.GetString(0)));
Image img = new Image();
img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
divHtml.Append(img);
divHtml.Append("</div>");
}
test1.InnerHtml = divHtml.ToString();
I get a funky output tho?
See picture:

Ive also tryed this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Web.UI.WebControls.Image;
using System.Web.UI.HtmlControls.HtmlGenericControl;
using System.IO;
public partial class UserProfileWall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string theUserId = Session["UserID"].ToString();
PopulateWallPosts(theUserId);
}
public static string RenderHtmlString(this System.Web.UI.HtmlControls.HtmlGenericControl htmlControl)
{
string result = string.Empty;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
var writer = new System.Web.UI.HtmlTextWriter(sw);
htmlControl.RenderControl(writer);
result = sw.ToString();
writer.Close();
}
return result;
}
private void PopulateWallPosts(string userId)
{
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
var divHtml = new System.Text.StringBuilder();
while (reader.Read())
{
using (StringWriter sw = new StringWriter())
divHtml.Append("<div id=test>");
divHtml.Append(String.Format("{0}", reader.GetString(0)));
Image img = new Image();
img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
divHtml.Append(img.RenderHtmlString());
//this line
divHtml.Append("</div>");
}
test1.InnerHtml = divHtml.ToString();
}
}
}
}
But RenderHtmlString has no definition?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
HtmlGenericControl("div") divHtml = new HtmlGenericControl("div");
divHtml.Controls.Add( HtmlImage image = new HtmlImage{ Src = "pathgoeshere" } );
Presumably you will need to also return this as a string? If so, try this handy Generic http://www.refactory.org/s/render_html_string_from_htmlgenericcontrol/view/latest but really do what everyone else is suggesting and use the power of <asp:PlaceHolder to it’s fullest and just add this returned control to the placeholder’s control collection.
Method 2
No, you cannot add a server control to a literal representation of a DOM element.
I am not sure what type of control “test1” is, but you should change it out to a PlaceHolder control and do the following (not tested!):
using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
var divHtml = new System.Text.StringBuilder();
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.ID = "test";
div.InnerHtml = String.Format("{0}", reader.GetString(0));
Image img = new Image();
img.ImageUrl = "~/images/test.jpg";
img.AlternateText = "Test image";
div.Controls.Add(img);
test1.Controls.Add(div);
}
test1.InnerHtml = divHtml.ToString();
}
}
Hope this helps!
Method 3
Just a bit of a concern. You’re dynamically building your SQL string. Make sure you sanitize the “userid” variable.
You can dynamically create images by doing this:
System.Web.UI.WebControls.Image img = new Image(); img.ImageUrl = "~/img/image1.jpg";
Do you know the images you wish to use already? Are the images or the image source located within your database?
Method 4
Try using a placeholder
Image img = new Image(); // instantiating the control img.ImageUrl = "~/myimage.gif"; // setting the path to the image placeholderpicture.Controls.Add(img);
From http://forums.asp.net/t/1308158.aspx/1
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0