一个简单的留言板的实现

来源:互联网 发布:基于java的毕业设计 编辑:程序博客网 时间:2024/05/17 12:46

1新建一个数据库BBS,新建一张表BBSItems.表设计如下:

 

 

 
2向APP_Code文件夹中添加一个BBSItem类,用于表示用户在BBS上发表的留言。
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///BBSTtem 的摘要说明
/// </summary>
public class BBSTtem
{
    public int BBSItemID = 0;  //主键
    private string _BBSContent = "";
    public string BBSContent   //留言内容
    {
        get { return _BBSContent ; }
        set
        {
            if (value.Length > 1000)
                _BBSContent = value.Substring(0, 1000); //诺留言超过固定长度1000,则只取前1000字符
            else
                _BBSContent = value;
        }
    }
    public DateTime SubmitTime = DateTime.Now;
    private string _NickName = "匿名";
    public string NickName
    {
        get { return _NickName; }
        set
        {
            if (value.Length > 50)
                _NickName = value.Substring(0, 50);
            else
                _NickName = value;
        }
    }
 public BBSTtem()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
       
 }
}
3 再向APP_Code中添加一个类BBSItemAccessObj类,负责从数据库中提取信息。
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;
/// <summary>
///BBSItemAccessObj 的摘要说明
/// </summary>
public class BBSItemAccessObj
{
 public BBSItemAccessObj()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    //根据DataRow中存储的信息更新一个BBSItem对象
    private void LoadFromDataRow(BBSTtem obj, DataRow dr)
    {
        if ((obj == null) || (dr == null))
            return;
        obj.BBSItemID = dr.IsNull("BBSItemID") ? 0 : (int)dr["BBSItemID"];
        obj .BBSContent =dr.IsNull ("BBSContent")?"":dr["BBSContent"] as string ;
        obj.NickName = dr.IsNull("NickName") ? "" : dr["NickName"] as string;
        obj .SubmitTime =dr.IsNull ("SubmitTime")?DateTime .Now :(DateTime )dr["SubmitTime"];
    }
     
    //将一个BBSItem对象插入到数据库中
    public void SaveBBSItemToDB(BBSTtem obj)
    {
        if (obj == null) return;
        string strsql = "insert into BBSItems(BBSContent,NickName,SubmitTime) ";
        strsql += "values(@BBSContent,@NickName,@SubmitTime)";
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=BBS;Integrated Security=True";
        conn.Open();
        SqlCommand comm = new SqlCommand(strsql ,conn );
        //添加参数
        comm.Parameters.AddWithValue("@BBSContent",obj .BBSContent );
        comm.Parameters.AddWithValue("NickName", obj.NickName);
        comm.Parameters.AddWithValue("SubmitTime", obj.SubmitTime);
        comm.ExecuteNonQuery();
        conn.Close();
       
    }
}
4 Default.aspx中的代码如下
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:GridView ID="GridView1" runat="server"
     AutoGenerateColumns ="False"
      Width="270px"
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
        CellPadding="2" ForeColor="Black" GridLines="None"
    >
    <Columns >
    <asp:TemplateField >
    <ItemTemplate >
    <asp:Label ID ="LblItemInfo" runat ="server" Text ='<%# Eval("BBSContent") %>'></asp:Label><br />
    发表于:<asp:Label ID="LbiSubmitTime" runat ="server" Text ='<%# Eval("SubmitTime") %>'></asp:Label><br />
   
    <asp:Label ID="LblItemContent" runat ="server" Text ='<%# Eval("NickName") %> '></asp:Label> <hr />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
        <FooterStyle BackColor="Tan" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
            HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
    </asp:GridView>
    <asp:TextBox ID="txtContent" runat="server"
        ontextchanged="TextBox1_TextChanged"></asp:TextBox>
    <p>
        <asp:TextBox ID="txtNickName" runat="server"
            ontextchanged="TextBox2_TextChanged"></asp:TextBox>
    </p>
    <asp:Button ID="btnSubmit" runat="server" onclick="Button1_Click"
        Text="Button" />
    </form>
</body>
</html>
5 Default.aspx.cs中的代码如下
 using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BBSItemAccessObj accessor = new BBSItemAccessObj();
            databind();
        }
    }
  
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BBSTtem item = new BBSTtem();
        if (txtNickName.Text.Trim().Length == 0)
            item.NickName = "匿名";
        else
            item.NickName = txtNickName.Text.Trim();
        item.BBSContent = txtContent.Text;
        item.SubmitTime = DateTime.Now;
        BBSItemAccessObj accessor = new BBSItemAccessObj();
        accessor.SaveBBSItemToDB(item);
        databind();
        txtNickName.Text = "";
        txtContent.Text = "";
    }
    void databind()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.;Initial Catalog=BBS;Integrated Security=True";
        string strsql = "select * from BBSItems order by SubmitTime DESC";
        SqlDataAdapter ad = new SqlDataAdapter(strsql, con);
        DataSet set = new DataSet();
        con.Open();
        ad.Fill(set);
        this.GridView1.DataSource = set.Tables[0];
        this.GridView1.DataBind();
        con.Close();
    }
}
6 效果图
原创粉丝点击