Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4241214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:16:27+00:00 2026-05-21T03:16:27+00:00

why can images not be appended to a div in asp? divHtml.append(img); why do

  • 0

why can images not be appended to a div in asp?

divHtml.append(img);

why do I have to use div.controls.add(img);?

and why cant I add a string to controls.add say like this

div.controls.add(img + String.Format("{0}", reader.GetString(0));

?
Orginally “In the beginning”

I had this code:

    cn.Open();
    using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + theUserId + " 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();

    }
}

}

Which gave me this out put (notice the css is applied and all spacing etc is nice and neat:

enter image description here

Then I did this code:

            while (reader.Read())
            {
                System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                //div.ID = "test";
                div.Style["float"] = "left";
                Image img = new Image();
                img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
                img.AlternateText = "Test image";
                div.Controls.Add(img);
                test1.Controls.Add(div);

                System.Web.UI.HtmlControls.HtmlGenericControl div1 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                //div1.ID = "test";
                div1.InnerText = String.Format("{0}", reader.GetString(0));

                div1.Style["float"] = "left";
                test1.Controls.Add(div1);

                System.Web.UI.HtmlControls.HtmlGenericControl div2 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                //div2.ID = "test";
                div2.Style["clear"] = "both";
                test1.Controls.Add(div2);
            }
        }
    }
}

}

And this was my result which is ok but if you notice carefully there is no css between each comment the divs are outside the realm of my css I tryed applying the commented out lines to see if it would work but its just abit funky tbh. Specialy when you look at it in firebug:

enter image description here
This is what happens if I try parse the control using the method mentioned below:

    cn.Open();
    using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
    {
        using (OdbcDataReader reader = cmd.ExecuteReader())
        {
            test1.Controls.Clear();

            while (reader.Read())
            {
                System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");

                div.ID = "test";
                div.Style["float"] = "left";
                Image img = new Image();
                img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
                img.AlternateText = "Test image";

                div.Controls.Add(img);
                div.Controls.Add(ParseControl(String.Format("{0}", reader.GetString(0))); 

                test1.Controls.Add(div);

enter image description here

Edit:

        using (OdbcDataReader reader = cmd.ExecuteReader())
        {
            test1.Controls.Clear();

            while (reader.Read())
            {
        System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
        div.Attributes["class"] = "test";
        div.Style["float"] = "left";

        div.ID = "test";
        //div.Style["float"] = "left";
        Image img = new Image();
        img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
        img.AlternateText = "Test image";

        div.Controls.Add(img);
        div.Controls.Add(ParseControl(String.Format("{0}", reader.GetString(0)))); 

        test1.Controls.Add(div);

Shadow managed to get this to work and I used the parse from the other comment aswell.

But problem remains now with test1 div not expanding with test divs:

enter image description here

        <asp:Button ID="Button1" runat="server" Text="Post Message" Width="98px" 
            onclick="Button1_Click" />
        </p>
    <p>
    </p>
        <div id="test1" runat="server" />
//could be this line
    </asp:Content>

Or it could be

test1.Controls.Add(div); 

in my code thats not being picked up or in the correct brackets maybe?
css:

div#test1 
{

}
div .test
{
  width:90%; 
  z-index:1; 
  padding:27.5px; 
  border-top: thin solid #736F6E;
  border-bottom: thin solid #736F6E;
  color:#ffffff;
  margin:0 auto;
  white-space: pre;
  white-space: pre-wrap;
  white-space: pre-line;
  word-wrap: break-word;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:16:28+00:00Added an answer on May 21, 2026 at 3:16 am

    You want to add image plus literal text coming from database.

    The proper way is indeed adding controls – Image control like you already have and for the text use Literal server control:

    div.Controls.Add(img);
    div.Controls.Add(new LiteralControl(reader.GetString(0)));
    

    Edit: if you insist on getting the HTML string of the image control for your own reasons, it’s possible using such code:

    StringBuilder html = new StringBuilder();
    using (StringWriter stringWriter = new StringWriter(html))
    {
        using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
        {
            image.RenderControl(textWriter);
        }
    }
    

    This will render the image into html variable, then you can assign the string value to the Text of some other control like you originally did. For example:

    myLabel.Text = html.ToString();
    

    Edit II: to apply CSS to the controls you’re adding, first change the CSS itself from this: div#test to this instead: div .test

    Now add this single line to your code:

    System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
    div.Attributes["class"] = "test";
    div.Style["float"] = "left";
    ...
    

    This will apply class to each top level div and this can be used to apply the proper CSS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a camera that returns raw images that can easily be converted to
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
Im trying to record video using AVFoundation I can save images but not video.
How can one load custom (not an image, nor a sound file) resource file
How can I fetch images from a server? I've got this bit of code
Can I get the images used in the self created theme direct or do
Is there an app that can change the order of images inside an icon?
Does anyone know of a way i can extract all jpg images from a
I want to print styled html pages with their images from a script. Can
So, I can create an input button with an image using <INPUT type="image" src="/images/Btn.PNG"

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.