通过代码隐藏在div中的图像

时间:2011-03-24 11:49:24

标签: c# asp.net html css stylesheet

我正试图在我的div id = test中设置我的图像这已成为极其成问题:

    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())
            {
                Image img = new Image();
                img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
                divHtml.Append("<div id=test>");
                divHtml.Append(img + String.Format("{0}", reader.GetString(0)));
                // how can I append an img inside my div id=test? 
                // the image must stay at the start of the div id=test + the contents(text) from my database (in that order)
                divHtml.Append("</div>");
            }
            test1.InnerHtml = divHtml.ToString();
        }
    }
}

}

的CSS:

* { padding: 0; margin: 0; outline: 0; }

body {
    font-size: 12px;
    line-height: 1.2;
    font-family: Arial, "Trebuchet MS", sans-serif;
    color: #a0a0a0;
    background: url(images/bg.gif) repeat 0 0;
    text-align: left;
    }
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;
}

Asp html:

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js" type="text/javascript"></script>
    <p>
        <asp:TextBox ID="TextBox1" name="TextBox1" runat="server" Rows="3" 
            Height="47px" Width="638px"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="Button1" runat="server" Text="Post Message" Width="98px" 
            onclick="Button1_Click" />
        </p>
    <p>
    </p>
        <div id="test1" runat="server" />
// contents from my code go inside this div as div id=test
    </asp:Content>

Firebug输出:

 <div id="ctl00_ContentPlaceHolder1_ContentPlaceHolder2_test1"><div id="test">System.Web.UI.WebControls.Imageweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee</div><div id="test">System.Web.UI.WebControls.Imagehello</div><div id="test">System.Web.UI.WebControls.Imagestill trying</div><div id="test">System.Web.UI.WebControls.Imageback to front on comments</div><div id="test">System.Web.UI.WebControls.Imageback to front on comments</div><div id="test">System.Web.UI.WebControls.Imageback to front on comments</div><div id="test">System.Web.UI.WebControls.Imageback to front on comments</div><div id="test">System.Web.UI.WebControls.Imagelets try this again</div><div id="test">System.Web.UI.WebControls.Imagehair marry went up the hill</div><div id="test">System.Web.UI.WebControls.Imagewedfwedwe</div><div id="test">System.Web.UI.WebControls.Imagewedfwedwe</div><div id="test">System.Web.UI.WebControls.Imagekjgfkjh</div><div id="test">System.Web.UI.WebControls.Imageanother comment</div><div id="test">System.Web.UI.WebControls.Imagebla bla hope this works</div></div>

我知道我的代码输出web ui控件的实际文本的原因,我已经在另一个实际获取图像的帖子代码中但它将图像添加到“END”测试div的内容,而不是它看起来像这样:

<div id="ctl00_ContentPlaceHolder1_ContentPlaceHolder2_test1">
<div id="test"><img src=blabla></img> the text goes after the image </div>

看起来像这样:

<div id="ctl00_ContentPlaceHolder1_ContentPlaceHolder2_test1">
<div id="test"> text comes before image </div>
<img src=blabla></img>
<div id="test">hello</div>
<img src=blabla></img>

这不是我想要的,我添加的代码虽然我知道它输出网页控件文本的原因只是一个“如何”我试图让它工作的演示。

divTest.Append on img THEN text;

当然我无法追加我必须使用controls.add,但我仍然无法让它工作。

上一篇文章就在这里,您可以看到一些图片和一些想法: My code below gives me a problem, the image comes after the text

1 个答案:

答案 0 :(得分:1)

Image是一个网络控件。你不能把它贴在StringBuilder