【问题标题】:How to make a Comment reply System using asp.net.?如何使用asp.net进行评论回复系统?
【发布时间】:2013-04-02 18:09:56
【问题描述】:

我想在 div 上显示一个帖子,并在该帖子下显示该帖子的回复,并连续想显示另一个帖子和回复... 我做了很多,但这并没有显示出所需的输出

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;

    public partial class Default3 : System.Web.UI.Page
    {
        int i = 0;
        SqlConnection con = new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2");
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from t3 ORDER by id DESC", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                i++;
                string name = dr["name"].ToString();
                string image1 = dr["img"].ToString();
                string image2 = image1.Substring(2).ToString();
                string id = dr["id"].ToString();
                string status = dr["status"].ToString();
                string buttonid = i.ToString();

                Response.Write("<div id='myDiv' class='myDivClass'> <img src='" + image2 + "' style='width:50px; height:50px; float:left; margin-right:6px;' /> <br/> <br/> <span class='name'> <input id='texta' class='texta' type='text' value=" + name.ToString() + " /> <span/> <span class='id'> <input id='textb' class='textb' type='text' value=" + id.ToString() + " /> <span/> <div id='g'><br/><br/><br/><span class='status'> " + status + " </span> <span class='combutton'> <input id='"+buttonid+"'  class='button' type='button' value='Comment'/> </span> </div> </div>");
            }
            con.Close();
        }
    }

【问题讨论】:

  • 以及当我使用页面中未显示的 Response.Write("any asp tag");
  • 如果您不让我们猜测它做了什么,以及输出有什么问题,您将获得更好的帮助。
  • 一般情况下,不要在Page 中使用Response.Write()。请改用网络控件。

标签: c# asp.net c#-4.0


【解决方案1】:

当基于您的 SQL 查询输出 HTML 时,您没有引用值的文本:

value=" + id.ToString() + " />

应该是这样的

   value='" + id.ToString() + "' />

除此之外,您还必须提供更多信息,说明未按预期显示的内容。但是,我可以解决以下几点:

Response.Write("任何asp标签")

ASP 标记是在服务器端处理的,而不是在客户端处理的。将它们写入响应不会导致它们被执行。如果您在浏览器中的 HTML 上“查看源代码”,您会在 HTML 源代码中看到标签,但它们对浏览器没有任何特殊意义。

请处理实现 IDisposable 的东西,例如 SqlConnection、SqlCommand 和 SqlDataReader。 using 关键字是去那里的方法。例如与

SqlConnection con = new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2");

线

con.Close();

如果访问数据库抛出异常,则不会执行。

改为使用表单

using (SqlConnection con = 
     new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2"))
{
    // Do stuff with the connection
}

using 块超出范围时,它会自动调用con.Dispose()(无论块内是否抛出异常),然后调用con.Close()

server=LOD-PC;uid=sa

请不要与sa 用户联系。那是要求安全马裤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多