显示所有评论innerHtml Asp.net

时间:2016-09-06 10:57:09

标签: asp.net innerhtml

我想写一些来自数据库的评论但写了最后一条评论..

我的代码:

 public void ShowComments(int id)
    {
        using (con = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString()))
        {
            using (cmd = new OracleCommand("select name,comments from comments where id=" + id + "", con))
            {
                con.Open();
                OracleDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {

                    comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";
                }

                con.Close();
            }
        }
    }

写下最后一条评论,我希望所有评论都写到&lt; div&gt; ..

    comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";

ASPX:

<div id="comments" runat="server">

</div>

1 个答案:

答案 0 :(得分:0)

comments.InnerHtml = "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";

它会覆盖每行comments.InnerHtml中存储的字符串,因此只显示最后一行。您需要使用+=运算符附加到该字符串,而不是覆盖该字符串。

comments.InnerHtml += "<br/>" + dr["name"] + ":" + dr["comments"] + "<br/>";