开发者

Adding asp.net image to div

开发者 https://www.devze.com 2023-02-19 19:11 出处:网络
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

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:

Adding asp.net image to div

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())
            {
     开发者_JS百科           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?


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.


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!


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消