购物网第三阶段总结笔记1:商品表的设计和后台产品分类界面制作

商品表Shop_product的设计:

分析静态页面可以知道:

1:每件商品都属于一个二级分类,因此要建立一个类别表Shop_category来存储商品的二级分类,此表包含的字段:id,类名,类别是一级还是二级,创建时间

shop_categroy: id,caname,pid,createdate

购物网第三阶段总结笔记1:商品表的设计和后台产品分类界面制作_第1张图片

2:商品表Shop_product字段:id,创建时间,商品名称,商品图片.......是否是特价商品,是否是新品,是否是VIP商品(默认值:0否  1是).....

shop_product:id,creatdate,proname,proimg,marketprice,memberprice,vipprice,state,norm,dw(单位),

caid,prodesc(商品描述),istj(是否特价),isxp(是否新品),isvip(是否vip)


购物网第三阶段总结笔记1:商品表的设计和后台产品分类界面制作_第2张图片


3:为商品建立一个评论表Shop_propl:id,评论时间,ip,商品名称,评级........

shop_propl:id,createdate,username,title,body,pj(评级:总共5级,默认5),ip,proid(被评论的商品id)

 购物网第三阶段总结笔记1:商品表的设计和后台产品分类界面制作_第3张图片

后台产品分类界面制作

【一】:使用动软代码生成器生成代码,然后复制到项目中,最后修改一下,把分页代码复制过去即可。

【二】:建立产品分类页面categroy.aspx,用于对产品分类的增删查改。 

(1):界面设计:

1:为dl增加样式,增加个边框

 <style type="text/css">
    dl
    {
        border:1px solid blue;
        width:350px;
        margin:5px;//为每个dl之间添加5个像素的间隙
        float:left;//左浮动,让每一个dl都靠左显示
        
        }
    </style>

2:清除样式,防止此样式影响到<fieldset>

  <div style="clear:both;"></div>
3:每一个dl作为一个产品分类:
<dl>
    <dt>
    <asp:TextBox ID="TextBox2" runat="server" Text="电脑配件"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="修改" />
        <asp:Button ID="Button3" runat="server" Text="删除" />
    </dt>
    <dd>
       <asp:TextBox ID="TextBox3" runat="server" Text="CPU"></asp:TextBox>
        <asp:Button ID="Button4" runat="server" Text="修改" />
        <asp:Button ID="Button5" runat="server" Text="删除" />
    </dd>
    <dd>
       <asp:TextBox ID="TextBox4" runat="server" Text="内存"></asp:TextBox>
        <asp:Button ID="Button6" runat="server" Text="修改" />
        <asp:Button ID="Button7" runat="server" Text="删除" />
    </dd>
    <dd>
       <asp:TextBox ID="TextBox5" runat="server" Text="硬盘"></asp:TextBox>
        <asp:Button ID="Button8" runat="server" Text="修改" />
        <asp:Button ID="Button9" runat="server" Text="删除" />
    </dd>
    <dd>
       <asp:TextBox ID="TextBox6" runat="server" Text="主板"></asp:TextBox>
        <asp:Button ID="Button10" runat="server" Text="修改" />
        <asp:Button ID="Button11" runat="server" Text="删除" />
    </dd>
    </dl>

4:增加产品分类的设计:

 <fieldset>
    <legend style="height: 14px; width: 89px">产品分类</legend>
        <br />
        分类级别:<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Selected="True" Value="0">一级分类</asp:ListItem>//此控件默认为一级分类,一级分类和二级分类值分别为:0和1
            <asp:ListItem Value="1">二级分类</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        父级:<asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br />
        分类名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </fieldset>

注意:①:点击RadioButtonList,让其自动返回

AutoPostBack="true"
②:当选择一级分类的时候,父级是没有的,因此把父级刚在Panel控件中,控制它的显示和隐藏:

 <asp:Panel ID="Panel1" runat="server" Visible="false">
                父级:<asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        </asp:Panel>

最后:

  <fieldset>
    <legend style="height: 14px; width: 89px">产品分类</legend>
        <br />




        分类级别:<asp:RadioButtonList ID="rad" runat="server" 
            RepeatDirection="Horizontal" RepeatLayout="Flow" AutoPostBack="true" 
            onselectedindexchanged="rad_SelectedIndexChanged">
            <asp:ListItem Selected="True" Value="0">一级分类</asp:ListItem>
            <asp:ListItem Value="1">二级分类</asp:ListItem>
        </asp:RadioButtonList>
        <br />






        <asp:Panel ID="Panel1" runat="server" Visible="false">
                父级:<asp:DropDownList ID="ddl" runat="server">
        </asp:DropDownList>
        </asp:Panel>


        <br />
        分类名称:<asp:TextBox ID="txtcaname" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnadd" runat="server" Text="增加" />
    </fieldset>


购物网第三阶段总结笔记1:商品表的设计和后台产品分类界面制作_第4张图片

总体设计就是上面的。



(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 categroy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //进入页面的时候绑定一级分类
                ddl.DataTextField = "caname";
                ddl.DataValueField = "id";
                ddl.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=0");
                ddl.DataBind();
            }
        }
        //点击当选按钮
        protected void rad_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rad.SelectedValue=="0")
            {
                Panel1.Visible = false;
            }
            else
            {
                Panel1.Visible = true;
            }
        }

        //增加分类
        protected void btnadd_Click(object sender, EventArgs e)
        {
            string caname = txtcaname.Text.Trim();
            if (caname.Length==0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请输入分类名称!')</script>");
                return;
            }

            string pid = rad.SelectedValue == "0" ? "0" : ddl.SelectedValue;
            //如果父级分类为空
            if (string.IsNullOrEmpty(pid))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请重新选择父级分类')</script>");
                return;
            }
            new MyShop.DAL.CategroyDAO().Add(new MyShop.Model.Categroy() { 
            caname=caname,
            pid=int.Parse(pid),
            createDate=DateTime.Now
            });

            //添加成功以后,重新加载本页面
            Response.Redirect(Request.Url.ToString());
        }
    }
}

(3):显示增加的分类:

观察上面设计好的显示分类的界面,可以知道,需要用两个Repeater控件进行嵌套,从而把数据从数据库中显示出来。

①:把aspx中多余的dl删除,只保留一个,用于Repeater控件中。

②:把Repeater进行嵌套:把rep1放进rep0中,为rep0添加ItemCommand事件。

                //绑定rep0数据

                rep0.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=0");
                rep0.DataBind();

       //循环rep0的每一行,在此处为rep1绑定数据
        protected void rep0_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Button btnMod = e.Item.FindControl("btnMod") as Button;
                string pid = btnMod.CommandArgument;//获取 btnMod传递的id
                Repeater rep1 = e.Item.FindControl("rep1") as Repeater;
                rep1.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=" + pid);
                rep1.DataBind();
            }
        }

此时的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 categroy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //进入页面的时候绑定一级分类
                ddl.DataTextField = "caname";
                ddl.DataValueField = "id";
                ddl.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=0");
                ddl.DataBind();


                //绑定rep0

                rep0.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=0");
                rep0.DataBind();
            }
        }
        //点击当选按钮
        protected void rad_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rad.SelectedValue=="0")
            {
                Panel1.Visible = false;
            }
            else
            {
                Panel1.Visible = true;
            }
        }

        //增加分类
        protected void btnadd_Click(object sender, EventArgs e)
        {
            string caname = txtcaname.Text.Trim();
            if (caname.Length==0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请输入分类名称!')</script>");
                return;
            }

            string pid = rad.SelectedValue == "0" ? "0" : ddl.SelectedValue;
            //如果父级分类为空
            if (string.IsNullOrEmpty(pid))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请重新选择父级分类')</script>");
                return;
            }
            new MyShop.DAL.CategroyDAO().Add(new MyShop.Model.Categroy() { 
            caname=caname,
            pid=int.Parse(pid),
            createDate=DateTime.Now
            });

            //添加成功以后,重新加载本页面
            Response.Redirect(Request.Url.ToString());
        }
       
       
        //循环rep0的每一行
        protected void rep0_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Button btnMod = e.Item.FindControl("btnMod") as Button;
                string pid = btnMod.CommandArgument;//获取 btnMod传递的id
                Repeater rep1 = e.Item.FindControl("rep1") as Repeater;
                rep1.DataSource = new MyShop.DAL.CategroyDAO().GetList("pid=" + pid);
                rep1.DataBind();
            }
        }
    }
}

(3):删除功能:

①:aspx中为两个删除按钮添加同一个事件Del

 OnClick="Del"

②:cs代码:

 //删除
        protected void Del(object sender, EventArgs e)
        {
            string id = (sender as Button).CommandArgument;
            if (dao.CalcCount("pid=" + id) > 0)
            {
                 Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('该节点下有子节点,不能删除,请先把子节点删除!')</script>");
                return;
            }

            dao.Delete(int.Parse(id));
            Response.Redirect(Request.Url.ToString());
        }


(3):修改功能:

①:aspx中为两个修改按钮添加同一个事件Mod

 OnClick="Mod"

②:cs代码:


        //修改
        protected void Mod(object sender, EventArgs e)
        {
            string id = (sender as Button).CommandArgument;
            //①:(sender as Button).NamingContainer:根据Button寻找到包含Button控件的上一级项目,在这里就是Repeater控件中的<ItemTemplate>
            //②:(sender as Button).NamingContainer.FindControl("txtCaname"):寻找<ItemTemplate>包含的txtCaname控件
            string caname=((sender as Button).NamingContainer.FindControl("txtCaname") as TextBox).Text;
            MyShop.Model.Categroy model = dao.GetModel(int.Parse(id));

            if (model!=null)
            {
                model.caname = caname;
                dao.Update(model);
                Response.Redirect(Request.Url.ToString());
            }
        }



【三】:最终代码:

aspx代码:


注意:两个Repeater控件中包含的控件的id可以相同。

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

<!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">
    dl
    {
        border:1px solid blue;
        width:350px;
        margin:5px;
        float:left;
        
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        产品分类</h1>



    <asp:Repeater ID="rep0" runat="server" onitemdatabound="rep0_ItemDataBound">
        <ItemTemplate>
            <dl>
                <dt>
                    <asp:TextBox ID="txtCaname" runat="server" Text='<%#Eval("caname") %>'></asp:TextBox>
                    <asp:Button ID="btnMod" runat="server" Text="修改" CommandArgument='<%#Eval("id") %>' OnClick="Mod" />
                    <asp:Button ID="btnDel" runat="server" Text="删除" CommandArgument='<%#Eval("id") %>' OnClick="Del" />
                </dt>


                <asp:Repeater ID="rep1" runat="server">
                    <ItemTemplate>
                        <dd>
                             <asp:TextBox ID="txtCaname" runat="server" Text='<%#Eval("caname") %>'></asp:TextBox>
                    <asp:Button ID="btnMod" runat="server" Text="修改" CommandArgument='<%#Eval("id") %>' OnClick="Mod" />
                    <asp:Button ID="btnDel" runat="server" Text="删除" CommandArgument='<%#Eval("id") %>' OnClick="Del" />
                        </dd>
                    </ItemTemplate>
                </asp:Repeater>


            </dl>
        </ItemTemplate>
    </asp:Repeater>


    <div style="clear: both;">
    </div>
    <fieldset>
        <legend style="height: 14px; width: 89px">产品分类</legend>
        <br />
        分类级别:<asp:RadioButtonList ID="rad" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"
            AutoPostBack="true" OnSelectedIndexChanged="rad_SelectedIndexChanged">
            <asp:ListItem Selected="True" Value="0">一级分类</asp:ListItem>
            <asp:ListItem Value="1">二级分类</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Panel ID="Panel1" runat="server" Visible="false">
            父级:<asp:DropDownList ID="ddl" runat="server">
            </asp:DropDownList>
        </asp:Panel>
        <br />
        分类名称:<asp:TextBox ID="txtcaname" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnadd" runat="server" Text="增加" OnClick="btnadd_Click" />
    </fieldset>
    </form>
</body>
</html>

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 categroy : System.Web.UI.Page
    {
       MyShop.DAL.CategroyDAO dao=new MyShop.DAL.CategroyDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //进入页面的时候绑定一级分类
                ddl.DataTextField = "caname";
                ddl.DataValueField = "id";
                ddl.DataSource = dao.GetList("pid=0");
                ddl.DataBind();


                //绑定rep0

                rep0.DataSource = dao.GetList("pid=0");
                rep0.DataBind();
            }
        }
        //点击当选按钮
        protected void rad_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rad.SelectedValue=="0")
            {
                Panel1.Visible = false;
            }
            else
            {
                Panel1.Visible = true;
            }
        }

        //增加分类
        protected void btnadd_Click(object sender, EventArgs e)
        {
            string caname = txtcaname.Text.Trim();
            if (caname.Length==0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请输入分类名称!')</script>");
                return;
            }

            string pid = rad.SelectedValue == "0" ? "0" : ddl.SelectedValue;
            //如果父级分类为空
            if (string.IsNullOrEmpty(pid))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请重新选择父级分类')</script>");
                return;
            }
            dao.Add(new MyShop.Model.Categroy()
            { 
            caname=caname,
            pid=int.Parse(pid),
            createDate=DateTime.Now
            });

            //添加成功以后,重新加载本页面
            Response.Redirect(Request.Url.ToString());
        }
       
       
        //循环rep0的每一行
        protected void rep0_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Button btnMod = e.Item.FindControl("btnMod") as Button;
                string pid = btnMod.CommandArgument;//获取 btnMod传递的id
                Repeater rep1 = e.Item.FindControl("rep1") as Repeater;
                rep1.DataSource = dao.GetList("pid=" + pid);
                rep1.DataBind();
            }
        }

        //删除
        protected void Del(object sender, EventArgs e)
        {
            string id = (sender as Button).CommandArgument;
            if (dao.CalcCount("pid=" + id) > 0)
            {
                 Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('该节点下有子节点,不能删除,请先把子节点删除!')</script>");
                return;
            }

            dao.Delete(int.Parse(id));
            Response.Redirect(Request.Url.ToString());
        }


        //修改
        protected void Mod(object sender, EventArgs e)
        {
            string id = (sender as Button).CommandArgument;
            //①:(sender as Button).NamingContainer:根据Button寻找到包含Button控件的上一级项目,在这里就是Repeater控件中的<ItemTemplate>
            //②:(sender as Button).NamingContainer.FindControl("txtCaname"):寻找<ItemTemplate>包含的txtCaname控件
            string caname=((sender as Button).NamingContainer.FindControl("txtCaname") as TextBox).Text;
            MyShop.Model.Categroy model = dao.GetModel(int.Parse(id));

            if (model!=null)
            {
                model.caname = caname;
                dao.Update(model);
                Response.Redirect(Request.Url.ToString());
            }
        }
    }
}


你可能感兴趣的:(object,server,asp,button,产品,textbox)