using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class article : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["my"] != null&&Request.QueryString["my"].ToString()!="") { showContent.Style.Add("display", "none");//不显示内容 comment.Style.Add("display", "none"); //不显示内容 replay.Style.Add("display", "none"); //不显示内容 h2.InnerHtml = "" + Session["username"].ToString() + "发表的帖子"; string sql = "select article.id,replaynum,readnum,title,username,classname,time from article,[user] where authorid='" + Request.QueryString["my"].ToString() + "' and userid=authorid order by article.id desc"; DataSet ds = DbManager.getDataSet(sql, "myArticle"); this.articlelist.DataSource = ds.Tables["myArticle"]; this.articlelist.DataBind(); DbManager.closeConnection(); } if (Request.QueryString["class"] != null && Request.QueryString["class"].ToString() != "") { showContent.Style.Add("display", "none");//不显示内容 comment.Style.Add("display", "none"); //不显示内容 replay.Style.Add("display", "none"); //不显示内容 h2.InnerHtml = "类别:" +Server.UrlDecode(Request.QueryString["class"].ToString()); string sql = "select article.id,replaynum,readnum,title,username,classname,time from article,[user] " + "where classname='" + Server.UrlDecode(Request.QueryString["class"].ToString()) + "' and userid=authorid order by article.id desc"; DataSet ds = DbManager.getDataSet(sql, "classArticle"); this.articlelist.DataSource = ds.Tables["classArticle"]; this.articlelist.DataBind(); DbManager.closeConnection(); } if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "") { int id = Convert.ToInt32(Request.QueryString["id"].ToString()); idValue.Value = id.ToString(); table.Style.Add("display", "none"); //不显示GridView控件 string sql = "select article.id,classname,title,authorid,time,context,userid,username,email from article,[user]"+ " where userid=authorid and article.id=" + id; SqlDataReader reader = DbManager.getReader(sql); if (reader.HasRows) { reader.Read(); string strContent = "

" + reader["title"].ToString() + "

"; strContent += "
" + reader["context"].ToString().Replace("\n", "
") + "
"; string author = reader["authorid"].ToString(); string classname = reader["classname"].ToString(); string userid = reader["userid"].ToString(); System.IO.StringWriter urlClass = new System.IO.StringWriter(); Server.UrlEncode(classname, urlClass); strContent += "
" + reader["time"].ToString() + "由 " + reader["username"].ToString() + " 撰写 类别:" + classname + "
"; strContent += "
评论 "; if (Session["userid"] != null && Session["userid"].ToString() == userid) { strContent += "编辑 "; strContent += "删除 "; } strContent += "Email

"; showContent.InnerHtml = strContent; if (!reader.IsClosed) reader.Close(); //更新阅读次数======================= string upsql = "update article set readnum=readnum+1 where id=" + id; DbManager.getNonQuery(upsql); //回复=============================== string replaySql = "select * from replay where articleid="+id; reader = DbManager.getReader(replaySql); if (reader.HasRows) { string strGuest = ""; int i = 1; while (reader.Read()) { strGuest += "
" + reader["guest"].ToString() + " 于 " + reader["time"].ToString() + " 跟帖 " + i + "楼" + reader["message"].ToString().Replace("\n", "
") + "
"; i++; } comment.InnerHtml = strGuest; reader.Close(); } else { comment.InnerHtml = "暂时没有回帖"; } } else { showContent.InnerHtml = "
文章不存在
"; } if (!reader.IsClosed) reader.Close(); DbManager.closeConnection(); //如果用户登录将其用户名写入留言文本框 if (Session["username"] == null || string.IsNullOrEmpty(Session["username"].ToString())) { // 未登录时隐藏整个评论区域 replay.Style["display"] = "none"; // 同时隐藏或禁用提交按钮(可选) addMessage.Visible = false; // 假设按钮ID为 addMessage } else { // 已登录时显示评论区域 replay.Style["display"] = "block"; addMessage.Visible = true; authorName.Text = Session["username"].ToString(); } } } } protected void addMessage_Click(object sender, EventArgs e) { // 没有登陆点击回复弹出登录框 if (Session["username"] == null) { Response.Redirect("Login.aspx"); return; } // 原有代码 int articleid = Convert.ToInt32(idValue.Value); string replaySql = "insert into replay(articleid,guest,message,time) values(" + articleid + ",'" + Session["username"].ToString() + "','" + content.Text + "','" + DateTime.Now.ToString() + "')"; int flag = DbManager.getNonQuery(replaySql); if (flag > 0) { string upsql = "update article set replaynum=replaynum+1 where id=" + articleid; if (DbManager.getNonQuery(upsql) > 0) Response.Redirect("replay.aspx?id=" + articleid); } else { Response.Redirect("replay.aspx?action=error"); } } protected string GetAvatarPath(string username) { // 统一返回默认头像路径 return "images/tx2.jpg"; } }