ASP.NET 简易新闻系统

一、新闻系统的设计

新闻系统由三个部分组成:

 二、文件组织结构如图:

 

三、数据库的设计 

本系统需要两张表:管理员信息表 tb_Admin 和 新闻信息表 tb_News。

管理员信息表 tb_Admin 主要用于保存管理员的信息。表结构如下:

 新闻信息表 tb_News 主要用于保存新闻的信息。表结构如下:

 四、数据库公共类的创建

为了便于数据库的操作,便于代码的复用,可将常用的数据库操作方法写到数据库公共类中,以便于页面的调用。

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

namespace 简易新闻系统
{
    public class dbClass
    {
        //定义连接对象
        private SqlConnection con;
        private string strCon = "server=.;database=news;uid=sa;pwd=123456;";

        //连接数据库
        public SqlConnection getCon()
        {
            con = new SqlConnection(strCon);
            con.Open();
            return con;
        }

        //关闭数据库
        public void con_Close()
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }

        //使用连接方式读取数据库
        public SqlDataReader getDr(string strSql)
        {
            getCon();
            SqlCommand cmd = new SqlCommand(strSql, con);
            SqlDataReader dr = cmd.ExecuteReader();
            return (dr);
        }

        // 连接式 对数据库进行增删改查操作
        public bool ExeNonQuery(string strSql)
        {
            getCon();
            SqlCommand cmd = new SqlCommand(strSql, con);
            if (cmd.ExecuteNonQuery() > 0)
            {
                con_Close();
                return true;
            }
            else
            {
                con_Close();
                return false;
            }
        }

        //查询单个值
        public object ExeScalar(string strSql)
        {
            getCon();
            SqlCommand cmd = new SqlCommand(strSql, con);
            object num = cmd.ExecuteScalar();
            con_Close();
            return (num);
        }

        //非连接式 对数据库进行增删改查操作
        public DataSet GetDataSet(string strSql, string tbName)
        {
            getCon();
            SqlDataAdapter da = new SqlDataAdapter(strSql, con);
            DataSet ds = new DataSet();
            da.Fill(ds, tbName);
            con_Close();
            return ds;
        }
    }
}

五、新闻系统的后台登录模块

页面设计:

 具体代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus.manage
{
    public partial class Login : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //登录按钮单击事件代码
        protected void btnOk_Click(object sender, EventArgs e)
        {
            string strSel = "select count(*) from tb_Admin where admin_Name='" + txtName.Text + "'and admin_Pwd='" + txtPwd.Text + "'";
            if (txtName.Text == "" || txtPwd.Text == "")
            {
                Response.Write("<script>alert('请输入用户名和密码');</script>");
            }
            else
            {
                if (Convert.ToInt32(db.ExeScalar(strSel)) > 0)
                {
                    Response.Write("<script>alert('您是合法用户');location.href='manage_List.aspx';</script>");

                }
                else
                {
                    Response.Write("<script>alert('用户名或密码输入错误');</script>");

                }
            }
        }
        //重置按钮单击事件代码
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
        }
        //注册按钮单击事件代码
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("manage_Register.aspx");
        }
    }
}

六、新闻系统的后台新闻管理模块

当管理员正确登录之后才可以看到管理内容。这部分主要是实现新闻的增删改查操作。

1.在管理员成功登录后,看到的第一个页面是新闻系统的管理页面 manage_List.aspx页面。

页面运行效果如图:

页面设计:添加一个label标签、一个GridView控件、两个button按钮。

“新闻内容”中的【详细信息】需要通过GridView中的添加新列操作来实现

设置HyperLinkField超链接列

 “新闻发布时间”字段中的时间只需要精确到 年-月-日,所以通过【编辑列】进行字段值的修改。

 

具体代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus.manage
{
    public partial class manage_List : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            //退出管理按钮代码 当前页面才管用
            //btnExit.Attributes.Add("onclick", "window.close()"); 
        }
        
        //页面切换事件代码,以帮助实现分页显示数据
        protected void gvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNews.PageIndex = e.NewPageIndex;
            gvNews.DataBind();
        }
        
        //添加新闻按钮事件代码
        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            Response.Redirect("manage_Add.aspx");
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("../index.aspx");
        }
        
        //删除事件代码
        protected void gvNews_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strDel = "delete from tb_News where news_Id=" + gvNews.DataKeys[e.RowIndex].Value;
            if (db.ExeNonQuery(strDel))
            {
                Response.Write("<script>alert('删除成功');location.href='manage_List.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('删除成功');</script>");
            }
            gvNews.DataBind();
        }

    }
}

2.新闻后台详细页面

详细页面根据传递的news_Id查询出相应的数据,并将其显示在页面中。

页面运行效果如图:

 根据manage_List.aspx页面中传过来的news_Id的值,来查询新闻的具体内容。news_Id的值用Requset对象来获取。

页面代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus.manage
{
    public partial class manage_Details : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strSel = "select * from tb_News where news_Id=" + Convert.ToInt32(Request["news_Id"]);
                DataSet ds = db.GetDataSet(strSel, "news");
                //获取新闻标题字段的值
                txtTitle.Text = ds.Tables["news"].Rows[0][1].ToString(); 
                //获取新闻类型字段的值
                lblNewsType.Text = ds.Tables["news"].Rows[0][2].ToString();
                //获取新闻内容字段的值
                txtContent.Text = ds.Tables["news"].Rows[0][3].ToString();
                //获取新闻发布时间字段的值
                lblNewsTime.Text = ds.Tables["news"].Rows[0][4].ToString().Substring(0,10);

            }
        }
        //返回按钮事件代码
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("manage_List.aspx");
        }
    }
}

3.添加新闻模块

页面设计:

 页面代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus.manage
{
    public partial class manage_Add : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //获取新闻的标题
            string strTitle = txtTitle.Text.ToString();
            //获取新闻的类型
            string strType = dropNewsType.SelectedValue.ToString();
            //获取新闻的内容
            string strContent = txtContent.Text.ToString();
            //插入语句
            string strIns = "insert into tb_News values('" + strTitle + "','" + strType + "','" + strContent + "','" + DateTime.Now.ToShortDateString() + "')";
            if (db.ExeNonQuery(strIns))
            {
                Response.Write("<script>alert('添加成功');location.href='manage_List.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败');</script>");

            }
        }
        //重置按钮事件代码
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTitle.Text = "";
            txtContent.Text = "";
        }
        //返回按钮事件代码
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("manage_List.aspx");
        }
    }
}

4.编辑新闻模块

首先应该将要修改的新闻内容显示出来,然后在已有的基础上修改。

为“新闻编辑”设置HyperLinkField超链接列:

 页面设计:

 页面代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus.manage
{
    public partial class manage_Update : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strSel = "select * from tb_News where news_Id=" + Convert.ToInt32(Request["news_Id"]);
                DataSet ds = db.GetDataSet(strSel, "news");
                //绑定字段
                txtTitle.Text = ds.Tables["news"].Rows[0][1].ToString();
                dropNewsType.SelectedValue= ds.Tables["news"].Rows[0][2].ToString();
                txtContent.Text= ds.Tables["news"].Rows[0][3].ToString();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strTitle = txtTitle.Text.ToString();
            string strType = dropNewsType.SelectedValue;
            string strContent = txtContent.Text.ToString();
            string strUpdate = "update tb_News set news_Title='" + strTitle + "',news_Type='" + strType + "',news_Content='" + strContent + "',news_AddTime='" + DateTime.Now.ToShortDateString().Substring(0, 10) + "'where news_Id="+Convert.ToInt32(Request["news_Id"]);
            if (db.ExeNonQuery(strUpdate)==true)
            {
                Response.Write("<script>alert('修改成功');location.href='manage_List.aspx';</script>");

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

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("manage_List.aspx");
        }
    }
}

七、新闻系统前台浏览模块

1.新闻浏览首页

页面控件:4个label控件、“更多新闻”用HyperLink控件、3个GridView控件

需要分别为GridView绑定新列:

注意:先进入“编辑列”对话框,取消“自动生成字段”的复选框。

页面设计如下:

 页面代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus
{
    public partial class index : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "新闻首页";
            if (!IsPostBack)
            {
                //国内新闻设置
                string strSel = "select top 5 * from tb_News where news_Type='国内新闻' order by news_AddTime Desc";
                DataSet ds1 = db.GetDataSet(strSel, "news_Home");
                int count1 = ds1.Tables["news_Home"].Rows.Count; //获取国内新闻的记录数量

                //依次判断新闻标题是否大于10个字符,若大于,额外的字符用“...”代替
                for (int j = 0; j < count1; j++)
                {
                    if (((string)ds1.Tables["news_Home"].Rows[j][1]).Length > 10)
                    {
                        ds1.Tables["news_Home"].Rows[j][1] = ds1.Tables["news_Home"].Rows[j][1].ToString().Substring(0, 10) + "...";
                    }
                }
                gvHome.DataSource = ds1.Tables["news_Home"];
                gvHome.DataBind();

                //财经新闻设置
                string strSel2 = "select top 5 * from tb_News where news_Type='财经新闻' order by news_AddTime Desc";
                DataSet ds2 = db.GetDataSet(strSel2, "news_Finance");
                int count2 = ds2.Tables["news_Finance"].Rows.Count;
                for (int j = 0; j < count2; j++)
                {
                    if (((string)ds2.Tables["news_Finance"].Rows[j][1]).Length > 10)
                    {
                        ds2.Tables["news_Finance"].Rows[j][1] = ds2.Tables["news_Finance"].Rows[j][1].ToString().Substring(0, 10) + "...";
                    }
                }
                gvFinance.DataSource = ds2.Tables["news_Finance"];
                gvFinance.DataBind();

                //娱乐新闻设置
                string strSel3 = "select top 5 * from tb_News where news_Type='娱乐新闻' order by news_AddTime Desc";
                DataSet ds3 = db.GetDataSet(strSel3, "news_Amusement");
                int count3 = ds3.Tables["news_Amusement"].Rows.Count;
                for (int j = 0; j < count3; j++)
                {
                    if (((string)ds3.Tables["news_Amusement"].Rows[j][1]).Length > 10)
                    {
                        ds3.Tables["news_Amusement"].Rows[j][1] = ds3.Tables["news_Amusement"].Rows[j][1].ToString().Substring(0, 10) + "...";
                    }
                }
                gvAmusement.DataSource = ds3.Tables["news_Amusement"];
                gvAmusement.DataBind();


            }
        }
    }
}

2.新闻详情页面

页面设计:

 页面效果:

 页面代码:

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;
using System.Data;

namespace 简易新闻系统plus
{
    public partial class news_Details : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strSel = "select * from tb_News where news_Id=" + Request.QueryString["news_Id"];
                DataSet ds = db.GetDataSet(strSel, "news");
                lblTitle.Text = ds.Tables["news"].Rows[0][1].ToString();
                lblType.Text= ds.Tables["news"].Rows[0][2].ToString();
                txtContent.Text= ds.Tables["news"].Rows[0][3].ToString();
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}

3.更多新闻页面

页面设计:

页面效果:

 页面代码:

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 System.Data.SqlClient;

namespace 简易新闻系统plus
{
    public partial class news_More : System.Web.UI.Page
    {
        dbClass db = new dbClass();
        //绑定数据源到GridView控件上
        protected void gvBind()
        {
            string strSel = "select * from tb_News where news_Type='" + Request.QueryString["news_Type"] + "'";
            DataSet ds = db.GetDataSet(strSel, "news");
            GridView1.DataSource = ds.Tables["news"];
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblType.Text = Request.QueryString["news_Type"].ToString();
                gvBind();    //调用绑定数据源的方法
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}

新闻管理系统(asp.net) 我开发了两天,开发好的。 主要缺点是不支持图片 欢迎大家修改完善 安装步骤: 1.首先您需要配置应用程序的运行环境。配置方法分为两步: (1)安装Internet 信息服务(需要用到系统安装光盘): 打开\"我的电脑\"-〉\"控制面板\"-〉\"添加或删除程序\"-〉点击左边的\"添加删除Windows组件\",在弹出的窗口中选择\"Internet 信息服务\"(IIS)(注:对于Windows Server 2003,\"Internet 信息服务\"被包含在应用程序服务器里边,只需要勾选 \"应用程序服务器\"即可),然后插入与当前系统相同的系统安装光盘,确定后开始安装。 (2)安装.Net Framework 2.0,下载地址:http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5 2.直接压缩本系统 装即可。如果希望新建一个虚拟目录或网站,请在网站上边点击右键选择新建虚拟目录(Windows服务器版本里有才有\"新建网站\"选项). 3.打开控制面板,管理工具,双击运行 Internet信息服务。依次打开网站 默认网站 找到虚拟目录,在右边右键选择main或是default.aspx,选择浏览即可。 asp.net常见错误及解决办法 1.错误描述:位于Config目录内的Web.sitemap文件格式不正确。或 操作必须有一个可更新的查询 分析:这可能是因为你使用了NTFS文件系统造成的,不恰当的NTFS授权也会导致这种错误。网站Config和App_Data两个文件夹需要有读写的权限。 解决办法:分别在Config和App_Data两个文件夹上点击右键,选择属性,选择安全选项卡,察看Everyone用户是否有修改的权限,如果没有请勾选。如果没有Everyone用户请依次点击添加、高级、立即查找,选中查找到的Everyone用户确定即可。 2.错误描述:无法显示 XML 页。使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。.... 分析:这可能是由于你没有正确安装.netframework 2.0的结果。 解决办法:打开IIS(即Internet信息管理器),在已安装的网站名称上边点击右键,选择属性,切换到Asp.net选项卡,察看ASP.Net version(即Asp.net版本)的选项是否为空,如果为空请从下拉列表选择2.0.50727版本或更高版本。 3.无法找到该页 分析:这可能是因为你使用的是Windows Server 2003操作系统。 解决办法:请打开IIS(Internet服务器),找到Web服务器扩展,设置Asp.Net为允许。 另一个可能的原因 可能是因为你在后台启用了Url重写功能,如果你设置了非aspx扩展名的文件扩展名,那么你需要手动在IIS(即Internet信息管理器)里边进行设置,具体方法如下: 首先在左边栏里需要设置的网站或虚拟目录上边点击右键并选择\"属性\",在\"虚拟目录\"选项卡中点击\"配置\",在打开的\"应用程序配置\"窗口中点击\"添加\",在可执行文件里边填写处理aspx文件的Dll文件路径,一般为c:\\windows\\microsoft.net\\framework\\v2.0.50727\\aspnet_isapi.dll,在扩展名里边填写你要模拟的文件扩展名,如.html.将\"动作\"限制为 GET,POST,并勾掉\"确认文件是否存在\"前边的勾即可。 4.如果仍然不能解决问题,请重新安装.net framework 2.0.
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值