购物网第一阶段总结笔记7:新闻管理模块之管理新闻(修改,删除)

1:和友情链接的管理一样,新建一个news_list.aspx页面,然后添加表格比便于显示数据,最后添加一个分页控件,以备后用
aspx最终代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="news_list.aspx.cs" Inherits="Web.admin.news_list" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!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>
    <style type="text/css">
        .style1 {
            width: 100%;
            border-style: solid;
            border-width: 1px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">

    <h1>
        <asp:Literal ID="litH1" Text="新闻中心列表" runat="server"></asp:Literal></h1><br />
    <table class="style1">
        <tr>
            <td>
                标题</td>
            <td>
                创建时间</td>
            <td>
                访问量</td>
            <td>
                操作</td>
        </tr>
        <asp:Repeater ID="replist" runat="server">
        <ItemTemplate>
        <tr>
            <td>
                <%#Eval("title") %></td>
            <td>
                <%#Eval("createDate") %></td>
            <td>
                <%#Eval("visitnum") %></td>
            <td>
                <a href='news_add.aspx?id=<%#Eval("id") %>'>修改</a>
                <asp:LinkButton ID="lbtnDel" OnClick="Del" CommandArgument='<%#Eval("id") %>' OnClientClick="return confirm('是否确定删除?')" runat="server">删除</asp:LinkButton>
                
                </td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
        
        <tr>
            <td colspan="4">
                <webdiyer:AspNetPager ID="anp" runat="server" CustomInfoHTML="总%PageCount%页,第%CurrentPageIndex%页/%PageCount%页" FirstPageText="首页" 
                    LastPageText="尾页" NextPageText="下一页" NumericButtonCount="5" PageSize="5" 
                    PrevPageText="上一页" ShowCustomInfoSection="Left" SubmitButtonText="GO" onpagechanged="anp_PageChanged">

                </webdiyer:AspNetPager>
            </td>
        </tr>
    </table>

    </form>
</body>
</html>

然后,在主页中添加“管理”的超链接,链接到这个页面
                新闻中心管理<br />
                <a href="news_add.aspx" target="frm">增加</a><br />
                <a href="news_list.aspx" target="frm">管理</a><br />
                <br />
                商品专题管理<br />
                <a href="news_add.aspx?type=spzt" target="frm">增加</a><br />
                <a href="news_list.aspx?type=spzt" target="frm">管理</a><br />


:2:CS代码:

第一步:实现从数据库调出数据,并以类别分开显示到商品专题和新闻中心的页面上,然后实现删除功能:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                BindRep();
            }
        }

        public string GetCond()
        {
            string cond = "";
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type) && type == "spzt")
            {
                cond = "type='商品专题'";
            }
            else
            {
                cond = "type='新闻中心'";
            }
            
            return cond;
        }

        private void BindRep()
        {
            replist.DataSource = new MyShop.DAL.NewsDAO().GetList(GetCond());
            replist.DataBind();
        }
        //删除
        protected void Del(object sender,EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            new MyShop.DAL.NewsDAO().Delete(int.Parse(id));
            BindRep();
        }

第二步:实现数据分页显示(分页显示控件)

1、修改NewsDAO表,和超链接管理的LinkDAO一样,做两项改动:修改一个分页显示函数  和添加一个计算记录数的函数

 /// <summary>
        /// 1: 分页获取数据列表
        /// </summary>
        /// <param name="fields">查询出要排序的字段</param>
        /// <param name="order">排序字段,以哪个字段为基准进行排序</param>
        /// <param name="ordertype">排序方式</param>
        /// <param name="PageSize">每页显示多少行数据</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="strWhere">查询where条件</param>
        /// <returns></returns>
        public DataSet GetList(string fields, string order, string ordertype, int PageSize, int PageIndex, string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("proc_SplitPage");//存储过程名
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Shop_news");//表名,传入的参数是固定的Shop_link表
            db.AddInParameter(dbCommand, "strFields", DbType.AnsiString, fields);//要查询出的字段
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);//多少页
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);//页码
            db.AddInParameter(dbCommand, "strOrder", DbType.String, order);//存储过程中的strOrder 字段,以哪个字段进行排序,类型改为String
            db.AddInParameter(dbCommand, "strOrderType", DbType.String, ordertype);//排序的方式,升序或者降序
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }


        /// <summary>
        /// 2:在LinkDAO中编写一个计算记录数的方法
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int CalcCount(string strWhere)
        {
            string sql = "select count(1) from Shop_news ";
            if (!string.IsNullOrEmpty(strWhere))
            {
                sql += " Where " + strWhere;//注意," Where "两边的必须有空格
            }
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            return int.Parse(db.ExecuteScalar(cmd).ToString());

        }

2、cs代码

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


namespace Web.admin
{
    public partial class news_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                anp.RecordCount = new MyShop.DAL.NewsDAO().CalcCount(GetCond());
                BindRep();
            }
        }


        public string GetCond()
        {
            string cond = "";
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type) && type == "spzt")
            {
                cond = "type='商品专题'";
                litH1.Text = "商品专题列表";
            }
            else
            {
                cond = "type='新闻中心'";
            }
            
            return cond;
        }


        private void BindRep()
        {
            replist.DataSource = new MyShop.DAL.NewsDAO().GetList("*", "createdate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());
            replist.DataBind();
        }
        //删除
        protected void Del(object sender,EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            new MyShop.DAL.NewsDAO().Delete(int.Parse(id));
            BindRep();
        }
        //分页事件
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            BindRep();
        }
    }
}

第三步:实现修改功能,当点击修改的时候,页面转跳到news_add.aspx,然后在这个页面中进行修改。

news_add的cs代码:

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

namespace Web.admin
{
    public partial class news_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Request.QueryString["type"];
                if (!string.IsNullOrEmpty(type) && type == "spzt")
                {

                    litH1.Text = "添加商品专题";

                }


                string id = Request.QueryString["id"];
                int x;
                if (int.TryParse(id, out x))
                {
                    MyShop.Model.News model = new MyShop.DAL.NewsDAO().GetModel(x);
                    if (model != null)
                    {
                        txttitle.Text = model.title;
                        txtbody.Text = model.body;
                        btnadd.Text = "修改";
                        litH1.Text = "修改";
                    }

                }
            }

        }

        protected void btnadd_Click(object sender, EventArgs e)
        {
            string title = txttitle.Text;
            string body = txtbody.Text;
            string type = Request.QueryString["type"];




            if (!string.IsNullOrEmpty(type) && type == "spzt")
            {
                type = "商品专题";

            }
            else
            {
                type = "新闻中心";
            }

            if (title.Length == 0 && body.Length == 0)
            {
                litmsg.Text = "<span style='color:red;'>新闻标题或者内容不能为空!</span>";
            }


            //修改新闻
            string id = Request.QueryString["id"];
            int x;
            if (int.TryParse(id, out x))
            {
                MyShop.Model.News model = new MyShop.DAL.NewsDAO().GetModel(x);
                if (model != null)
                {
                    model.title = title;
                    model.body = body;
                    new MyShop.DAL.NewsDAO().Update(model);
                    txttitle.Text = "";
                    txtbody.Text = "";
                    litmsg.Text = "<span style='color:bule;'>修改成功!</span>";
                    return;
                }
            }

            //添加新闻

            MyShop.DAL.NewsDAO dao = new MyShop.DAL.NewsDAO();
            int res = dao.Add(new MyShop.Model.News()
            {
                title = title,
                body = body,
                visitnum = 0,
                createDate = DateTime.Now,
                type = type
            });


            if (res > 0)
            {
                litmsg.Text = "<span style='color:bule;'>添加成功!</span>";
                txttitle.Text = "";
                txtbody.Text = "";
            }
            else
            {
                litmsg.Text = "<span style='color:red;'>添加失败,请联系管理员!</span>";
            }

        }
    }
}


第四步:实现查询功能

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

namespace Web.admin
{
    public partial class news_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                anp.RecordCount = new MyShop.DAL.NewsDAO().CalcCount(GetCond());
                BindRep();
            }
        }

        public string GetCond()
        {
            string cond = "";
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type) && type == "spzt")
            {
                cond = "type='商品专题'";
                litH1.Text = "商品专题列表";
            }
            else
            {
                cond = "type='新闻中心'";
            }


            if (txtkey.Text.Trim().Length!=0)
            {
                string key = txtkey.Text.Trim();

                cond = "title like '%" + key + "%'";     //此时cond的值把前面的cond的值覆盖了
            }
            return cond;
        }

        private void BindRep()
        {
            replist.DataSource = new MyShop.DAL.NewsDAO().GetList("*", "createdate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());
            replist.DataBind();
        }
        //删除
        protected void Del(object sender,EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            new MyShop.DAL.NewsDAO().Delete(int.Parse(id));
            BindRep();
        }
        //分页事件
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            BindRep();
        }

        //查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            anp.RecordCount = new MyShop.DAL.NewsDAO().CalcCount(GetCond());
            BindRep();
        }
    }
}

搜索的缺陷:当在商品专题中搜索时候,也会把新闻中心的东西搜索出来,同样,搜索新闻的时候,也会商品的东西搜索出来,这不是我们想要的结果。


改进一下:

        public string GetCond()
        {
            string cond = "";
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type) && type == "spzt")
            {
                cond = "type='商品专题'";
                litH1.Text = "商品专题列表";

                if (txtkey.Text.Trim().Length != 0)
                {
                    string key = txtkey.Text.Trim();

                    cond += "and title like '%" + key + "%'";
                }
            }
            else
            {
                cond = "type='新闻中心'";
                if (txtkey.Text.Trim().Length != 0)
                {
                    string key = txtkey.Text.Trim();

                    cond += "and title like '%" + key + "%'";
                }
            }


            return cond;
        }

这样就好了。


然后做新闻中心和商品专题的前台页面,很好做 ,不写笔记了


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值