有没有办法可以动态地将Image1添加到下面代码中的while循环中(包含在div中)我是说实际上是在div中添加了一个asp图像?通过代码。目前,当我看到代码寻找一个asp图像,但我看不出你可以“添加”它到我的动态内容:
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();
}
}
我以为我有这个:
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();
我得到了一个时髦的输出?
见图:
我也试过这个:
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();
}
}
}
}
但RenderHtmlString没有定义?
答案 0 :(得分:4)
HtmlGenericControl("div") divHtml = new HtmlGenericControl("div");
divHtml.Controls.Add( HtmlImage image = new HtmlImage{ Src = "pathgoeshere" } );
据推测,您还需要将其作为字符串返回?如果是这样的话,试试这个方便的Generic http://www.refactory.org/s/render_html_string_from_htmlgenericcontrol/view/latest,但要真正做到其他人的建议,并使用<asp:PlaceHolder
的强大功能,只需将此返回的控件添加到占位符的控件集合中。
答案 1 :(得分:3)
不,您无法将服务器控件添加到DOM元素的文字表示中。
我不确定“test1”是什么类型的控件,但是您应该将其更改为PlaceHolder控件并执行以下操作(未测试!):
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();
}
}
希望这有帮助!
答案 2 :(得分:1)
只是有点担心。您正在动态构建SQL字符串。确保清理“userid”变量。
您可以通过以下方式动态创建图像:
System.Web.UI.WebControls.Image img = new Image();
img.ImageUrl = "~/img/image1.jpg";
您知道您想要使用的图像吗?图像或图像源是否位于数据库中?
答案 3 :(得分:1)
尝试使用占位符
Image img = new Image(); // instantiating the control
img.ImageUrl = "~/myimage.gif"; // setting the path to the image
placeholderpicture.Controls.Add(img);