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?
Image.ToString()(what you're doing) returnsSystem.Web.UI.WebControls.Imageso you're getting what's expected. You need to render it as text. See my answer below for a link that includes a Generic method (that you put in any class, and then call asimg.RenderHtmlString()so you could use what you have and changedivHtml.Append(img);todivHtml.Append(img.RenderHtmlString());if you include that Generics method in your project.