弗尤博客(八)

好几天没更新博客了,毕业设计进度慢下了,做了几个网页耽搁了。但是还是完成了评论的基本功能。
我的思路还是以前一样通过datalist 来实现的,在显示博文详细页面时,登录情况下,用session来确保评论人,非登录情况下,用label显示需要登录后才可评论,评论内容后台调用datalist中的控件完成数据库的添加弗尤博客(七) 评论数量是倒叙查看数据库后取第一个,发评论时也是相同的,数目加一就好。

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using Entity;
public partial class UserBlogs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {


        if (Session["uid"] == null)
        {
            if (Request.QueryString["uid"] == null && Request.QueryString["bid"] == null)
            {

            }
            if (Request.QueryString["uid"] != null && Request.QueryString["bid"] == null)
            {
                Cind();
            }
            if (Request.QueryString["uid"] == null && Request.QueryString["bid"] != null)
            {
                Aind();
                Eind();
            }
        }
        else
        {

            if (Request.QueryString["uid"] == null && Request.QueryString["bid"] == null)
            {
                Bind();
            }
            if (Request.QueryString["uid"] != null && Request.QueryString["bid"] == null)
            {
                Cind();
            }
            if (Request.QueryString["uid"] == null && Request.QueryString["bid"] != null)
            {
                Aind();
                Dind();
            }
        }
    }
}

public void Aind()//该篇博文
{
    BlogsBusiness blogs = new BlogsBusiness();
    BlogsEntity b = new BlogsEntity();
    DataTable dt = new DataTable();
    b.Bid =Convert.ToInt32(Request.QueryString["bid"]);
    dt = blogs.SelectUid(b);
    DataList1.DataSource = dt;
    DataList1.DataBind();
    CommentEntity c = new CommentEntity();
    CommentBusiness comment = new CommentBusiness();
    DataTable data = new DataTable();
    c.Bid= Convert.ToInt32(Request.QueryString["bid"]);
    data = comment.SelectComment(c);
    DataList3.DataSource = data; 
    DataList3.DataBind();
    lbl.Text = "";
}
public void Bind()//我的博客
{
    BlogsBusiness blogs = new BlogsBusiness();
    BlogsEntity b = new BlogsEntity();
    DataTable dt = new DataTable();
    b.Uid = Session["uid"].ToString();
    dt = blogs.SelectBid(b);
    DataList2.DataSource = dt;
    DataList2.DataBind();
}
public void Cind()//他人的博客
{
    BlogsBusiness blogs = new BlogsBusiness();
    BlogsEntity b = new BlogsEntity();
    DataTable dt = new DataTable();
    b.Uid = Request.QueryString["uid"];
    dt = blogs.SelectBid(b);
    DataList2.DataSource = dt;
    DataList2.DataBind();
}
public void Dind()//评论
{
    UserBusiness user = new UserBusiness();
    UserEntity u = new UserEntity();
    DataTable d = new DataTable();
    u.Uid = Session["uid"].ToString();
    d = user.FindUserInfo(u);
    DataList4.DataSource = d;
    DataList4.DataBind();
}
public void Eind()//登录后可评论
{
    lbl.Text = "登录后可评论";
}





protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "blogs")
    {
        Response.Redirect("UserBlogs.aspx?bid=" + e.CommandArgument.ToString());
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    //TextBox t = new TextBox();
    //t=(TextBox)DataList4.FindControl("TextBox4");
    //Label l = new Label();
    //l = (Label)DataList3.FindControl("Label7");
    DataListItem item = (DataListItem)((Button)sender).NamingContainer;
    TextBox txt = item.FindControl("TextBox4") as TextBox;
    CommentBusiness comment = new CommentBusiness();
    CommentEntity c = new CommentEntity();
    c.Bid = Convert.ToInt32(Request.QueryString["bid"]);
    DataTable dt = new DataTable();
    dt = comment.SelectCid(c);
    int cidnums=Convert.ToInt32(dt.Rows[0][0]);
    cidnums = cidnums + 1;
    CommentEntity ce = new CommentEntity();
    ce.Cid = cidnums;
    //ce.Comment = txt.ToString();
    ce.Comment = txt.Text.ToString();
    ce.Bid= Convert.ToInt32(Request.QueryString["bid"]);
    ce.Uid= Session["uid"].ToString();
    ce.Ctime= DateTime.Now;
    int res = 0;
    res = comment.InsertComment(ce);
    if (res > 0)
    {
        //DataListItem items = (DataListItem)((Button)sender).NamingContainer;
        //Label lbl = items.FindControl("Label3") as Label;
        BlogsBusiness blogs = new BlogsBusiness();
        BlogsEntity b = new BlogsEntity();
        b.Comment = cidnums.ToString();
        b.Bid=Convert.ToInt32(Request.QueryString["bid"]);
        int r = 0;
        r = blogs.UpdateCommentNums(b);
        if (res > 0)
        {
            Aind();
            Dind();
            Response.Write("<script>alert('评论成功')</script>");
        }

    }
    else
    {
        Response.Write("<script>alert('评论失败')</script>");
    }





    }
}`
原文地址:https://www.cnblogs.com/frankybpx/p/10167729.html